2011-07-11 17 views
12

XmlDocumentクラスを使用して任意のXML要素に対して動的に値を設定できますか?私のXMLはXmlDocumentクラスを使用してxml要素の値を設定する方法

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
    <soapenv:Body> 
     <v9:ProcessShipmentReply xmlns:v9="http://fedex.com/ws/ship/v9"> 
      <v9:HighestSeverity xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">SUCCESS</v9:HighestSeverity> 
      <v9:Notifications xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
       <v9:Severity>SUCCESS</v9:Severity> 
       <v9:Source>ship</v9:Source> 
       <v9:Code>0000</v9:Code> 
       <v9:Message>Success</v9:Message> 
       <v9:LocalizedMessage>Success</v9:LocalizedMessage> 
      </v9:Notifications> 
      <v9:CompletedShipmentDetail> 
       <v9:CompletedPackageDetails xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
        <v9:SequenceNumber>1</v9:SequenceNumber> 
        <v9:TrackingIds> 
         <v9:TrackingIdType>GROUND</v9:TrackingIdType> 
         <v9:TrackingNumber>634649515000016</v9:TrackingNumber> 
        </v9:TrackingIds> 
        <v9:Barcodes> 
         <v9:BinaryBarcodes> 
          <v9:Type>COMMON_2D</v9:Type> 
          <v9:Value>Wyk+HjAxHTAyMDI3ODAdODQwHTEzNx02MzQ2NDk1</v9:Value> 
         </v9:BinaryBarcodes> 
         <v9:StringBarcodes> 
          <v9:Type>GROUND</v9:Type> 
          <v9:Value>9612137634649515000016</v9:Value> 
         </v9:StringBarcodes> 
        </v9:Barcodes> 
        <v9:Label> 
         <v9:Type>OUTBOUND_LABEL</v9:Type> 
         <v9:ShippingDocumentDisposition>RETURNED</v9:ShippingDocumentDisposition> 
         <v9:Resolution>200</v9:Resolution> 
         <v9:CopiesToPrint>1</v9:CopiesToPrint> 
         <v9:Parts> 
          <v9:DocumentPartSequenceNumber>1</v9:DocumentPartSequenceNumber> 
          <v9:Image>iVBORw0KGgoAAAANSUhEUgAAAyAAAASwAQAAAAAryhMIAAAagEl</v9:Image> 
         </v9:Parts> 
        </v9:Label> 
       </v9:CompletedPackageDetails> 
      </v9:CompletedShipmentDetail> 
     </v9:ProcessShipmentReply> 
    </soapenv:Body> 

私は

<v9:Severity>SUCCESS</v9:Severity> 
<v9:Source>ship</v9:Source> 

のような以下の要素に値を設定することができどのようにXMLからデータを抽出する方法を知っていると私は値を設定することも可能であると考えていると仮定XMLDocumentクラスを使用するXML要素の場合ガイダンスを探しています。

+0

どこにexcactlyこれらの要素を追加する必要がありますか? – HiperiX

答えて

22

値を選択する方法が分かっている場合は、値を更新する方法も分かっているはずです。

XmlDocument doc = new XmlDocument(); 
doc.Load(...); 
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable); 
nsMgr.AddNamespace("v9", "http://fedex.com/ws/ship/v9"); 

XmlNode severityNode = doc.SelectSingleNode("//v9:Severity", nsMgr); 
severityNode.innerText = "FAILURE"; 

知るべき重要なことは、あなたがNode.Valueプロパティを使用することはできません上記の例のように、<v9:Severity>ノードは、内部text()ノードを持っているということです。これを行うには、次のようにします。

XmlNode severityTextNode = doc.SelectSingleNode("//v9:Severity/text()", nsMgr); 
severityTextNode.Value = "FAILURE"; 

微妙な違いに注意してください。

+0

うわー.....ありがとう – Mou

0

XMLDocumentクラスを使用して上位ノードにXPathを行い、2つの余分なノードをツリーに追加します。

+0

Severity&Source要素に新しい値を追加するという既存の値を変更する必要があります。おそらくあなたは私の質問を理解していませんでした。 – Mou

関連する問題