2017-05-26 44 views
1

以下是我的cXML的數據來讀取XML節點列表中的子節點,並且能夠加載CXML數據從XML有效載荷要求xmlnodeslist,現在我想讀xmlnodeslist的所有子節點下面是代碼,我我正在使用將xml數據加載到xmlnodeslist和我的cXML數據中。如何使用C#

XmlDocument doc = new XmlDocument(); 
     doc.Load(request.Content.ReadAsStreamAsync().Result);   
     string s = doc.DocumentElement.OuterXml;   
     doc.LoadXml(s); 

     XmlNodeList nodes = doc.DocumentElement.SelectNodes("//Credential"); 

     StringBuilder yourString = new StringBuilder(); 
     yourString.Append("The Products available are : ");   
     foreach (XmlNode node in nodes) 
     { 
      yourString.Append(node["Name"].InnerText + ",");    
     } 
     log.Debug("hemanth===========> outer" + yourString); 
     return request.CreateResponse(HttpStatusCode.OK); 

這裏是我的cXML的數據

<?xml version="1.0"?> 
<cXML timestamp="2017-04-06T05:44:31-04:00" version="1.2.0025" payloadID="ddae78e3-20b0-4f9d-91b8-7ca854f27568" xml:lang="en-US"> 
    <Header> 
    <From> 
     <Credential domain="NetworkId"> 
     <Identity>FakeIdentity</Identity> 
     </Credential> 
    </From> 
    <To> 
     <Credential domain="NetworkId"> 
     <Identity>FakeIdentity1</Identity> 
     </Credential> 
    </To> 
    <Sender> 
     <Credential domain="NetworkId"> 
     <Identity>FakeIdentity3</Identity> 
     <SharedSecret>78910</SharedSecret> 
     </Credential> 
     <UserAgent>ServiceChannel Agent</UserAgent> 
    </Sender> 
    </Header> 
    <Request> 
    <OrderRequest> 
     <OrderRequestHeader orderID="79491774" orderDate="2017-02-01T05:07:33-05:00" type="new"> 
     <Total> 
      <Money currency="USD">700</Money> 
     </Total> 
     <ShipTo> 
      <Address addressID="0000000000"> 
      <Name xml:lang="en-US">2115</Name> 
      <PostalAddress name="Prudential Center"> 
       <DeliverTo>fatiha</DeliverTo> 
       <Street>800 Boylston Street Suite 179 800 Boylston Street Suite 179</Street> 
       <City>Boston</City> 
       <State>MA</State> 
       <PostalCode>02199</PostalCode> 
       <Country isoCountryCode="US">US</Country> 
      </PostalAddress> 
      <Email name="default">[email protected]</Email> 
      <Phone name="work"> 
       <TelephoneNumber> 
       <CountryCode isoCountryCode="US">1</CountryCode> 
       <AreaOrCityCode>617</AreaOrCityCode> 
       <Number>111111</Number> 
       </TelephoneNumber> 
      </Phone> 
      </Address> 
     </ShipTo> 
     <BillTo> 
      <Address addressID="0000000000"> 
      <Name xml:lang="en-US">2115</Name> 
      <PostalAddress name="Prudential Center"> 
       <DeliverTo>fatiha</DeliverTo> 
       <Street>800 Boylston Street Suite 179 800 Boylston Street Suite 179</Street> 
       <City>Boston</City> 
       <State>MA</State> 
       <PostalCode>02199</PostalCode> 
       <Country isoCountryCode="US">US</Country> 
      </PostalAddress> 
      <Email name="default">[email protected]</Email> 
      <Phone name="work"> 
       <TelephoneNumber> 
       <CountryCode isoCountryCode="US">1</CountryCode> 
       <AreaOrCityCode>617</AreaOrCityCode> 
       <Number>2476959</Number> 
       </TelephoneNumber> 
      </Phone> 
      </Address> 
     </BillTo> 
     <Extrinsic name="SubscriberID">2000000000</Extrinsic> 
     <Extrinsic name="StoreNumber">0000</Extrinsic> 
     <Extrinsic name="UserOrgDept">2000000000_0000</Extrinsic> 
     <Extrinsic name="StoreEmail">[email protected]</Extrinsic> 
     <Extrinsic name="StorePhone">000-000-0000</Extrinsic> 
     </OrderRequestHeader> 
     <ItemOut quantity="1" lineNumber="0"> 
     <ItemID> 
      <SupplierPartID>00000000</SupplierPartID> 
      <SupplierPartAuxiliaryID/> 
     </ItemID> 
     <ItemDetail> 
      <UnitPrice> 
      <Money currency="USD">41.4700</Money> 
      </UnitPrice> 
      <Description xml:lang="en-US">Heavy Duty Box Sealing Tape - CLEAR</Description> 
      <UnitOfMeasure>EA</UnitOfMeasure> 
      <Classification domain="SupplierPartID">00000000</Classification> 
     </ItemDetail> 
     </ItemOut> 
     <ItemOut quantity="2" lineNumber="1"> 
     <ItemID> 
      <SupplierPartID>000000</SupplierPartID> 
      <SupplierPartAuxiliaryID/> 
     </ItemID> 
     <ItemDetail> 
      <UnitPrice> 
      <Money currency="USD">33.1500</Money> 
      </UnitPrice> 
      <Description xml:lang="en-US">Padded Mailers 9-1/2"X14-1/2"</Description> 
      <UnitOfMeasure>EA</UnitOfMeasure> 
      <Classification domain="SupplierPartID">000000</Classification> 
     </ItemDetail> 
     </ItemOut> 
     <ItemOut quantity="1" lineNumber="2"> 
     <ItemID> 
      <SupplierPartID>000000</SupplierPartID> 
      <SupplierPartAuxiliaryID/> 
     </ItemID> 
     <ItemDetail> 
      <UnitPrice> 
      <Money currency="USD">27.5000</Money> 
      </UnitPrice> 
      <Description xml:lang="en-US">Bubble Mailers 12-1/2" x 19"</Description> 
      <UnitOfMeasure>EA</UnitOfMeasure> 
      <Classification domain="SupplierPartID">000000</Classification> 
     </ItemDetail> 
     </ItemOut> 
     <ItemOut quantity="1" lineNumber="3"> 
     <ItemID> 
      <SupplierPartID>00000000</SupplierPartID> 
      <SupplierPartAuxiliaryID/> 
     </ItemID> 
     <ItemDetail> 
      <UnitPrice> 
      <Money currency="USD">40.5000</Money> 
      </UnitPrice> 
      <Description xml:lang="en-US">Pallet Shrinkwrap</Description> 
      <UnitOfMeasure>EA</UnitOfMeasure> 
      <Classification domain="SupplierPartID">00000000</Classification> 
     </ItemDetail> 
     </ItemOut> 
    </OrderRequest> 
    </Request> 
</cXML> 

任何人的幫助能做到這一點,

感謝, Venkat。

+0

你似乎在閱讀完所有的子節點在for循環。你想要做什麼,你的子節點,你目前無法? –

+0

例如在這個我需要timestamp值和payloadId值..就像我需要從cxml的所有值?所以我如何得到,你可以請解釋....!在此先感謝,venkat – Venkat

+0

XmlNodeList nodes = doc.DocumentElement.SelectNodes(「// Credential」);你能建議我以上將努力讀取節點嗎? – Venkat

回答

0

試試這個:

string timeStamp = node.Attributes["timestamp"].Value; 

例如,這將打印出每一個憑證「域」和他們的身份值:

XmlNodeList nodes = doc.DocumentElement.SelectNodes("//Credential"); 

foreach (XmlNode node in nodes) 
{ 
    var identity = node.SelectSingleNode("Identity").InnerText; 
    yourString.Append(node.Attributes["domain"].Value + "|" + identity + ",");    
} 
+0

的foreach(在節點XmlNode的節點) { yourString.Append(節點[ 「名稱」]的innerText + 「」); }這裏的節點沒有得到任何東西,指針也沒有進入到每個塊 – Venkat

+0

首先,你必須找到你想要的確切節點。然後在我的答案中運行代碼。 –

+0

XmlNodeList nodes = doc.DocumentElement.SelectNodes(「// Credential」);你能建議我以上將努力讀取節點嗎? – Venkat