0

私は次の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>
4

1 に答える 1

1

これ<items>は、ルート要素の直接の子ではないためです。これをコンソールアプリに貼り付けると、何が起こっているかがわかります。

 var xd = XDocument.Load("xml.xml");

Console.WriteLine(xd.Root.Name); // {http://schemas.xmlsoap.org/soap/envelope/}Envelope
Console.WriteLine(xd.Root.Descendants("items").First().Name ); //items
Console.ReadKey();

Descendantsすべての子(および孫など)で名前の付いたアイテムをチェックし、Element直接の子のみを調べます。

子孫が深さ優先か幅優先かわからないので、巨大なドキュメントのパフォーマンスに注意する必要があるかもしれません。

于 2010-11-09T11:27:58.867 に答える