将 Xelement 附加到 Xdocument
我有以下 xdocument ,我尝试使用以下代码在 items 元素中附加 item 元素:
xdocument.Root.Element("items").add(item)
这不起作用,因为找不到 items 元素。我认为这是命名空间的问题,但我似乎无法让它工作。任何帮助将不胜感激。
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mynamespace.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getUpload>
<itemObj>
<items SOAP-ENC:arrayType="ns1:item[2]" xsi:type="ns1:ArrayOfItem">
<!--Item elements to go here-->
</items>
</itemObj>
</ns1:getUpload>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I have the following xdocument , I am trying to append item elements within the items element with the following code:
xdocument.Root.Element("items").add(item)
This does not work as the items element can not be found. I think it is a problem with the namespaces but I can't seem to get this to work. Any help will be much appreciated.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://mynamespace.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getUpload>
<itemObj>
<items SOAP-ENC:arrayType="ns1:item[2]" xsi:type="ns1:ArrayOfItem">
<!--Item elements to go here-->
</items>
</itemObj>
</ns1:getUpload>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您的
不是根元素的直接子元素。将其粘贴在控制台应用程序中会显示正在发生的情况:
“后代”检查所有子项(以及孙子项等)中名为的项目,“元素”仅查看直接子项。
我不确定后代是深度优先还是广度优先,因此您可能需要小心大型文档的性能。
It's because you
<items>
is not the direct child of your Root Element.Sticking this in a console app shows what is going on:
Descendants
checks through all children (and grand children etc) for the item named,Element
only looks at direct children.I am not sure on if Descendants is Depth First or Breadth First, so you may want to be careful on performance on huge documents.