How to read sub Nodes from xml nodes list using c#

177 views Asked by At

following is my cXML data, and able to load cxml data to xmlnodeslist from xml payload request, now i want to read all the sub nodes from xmlnodeslist following are the code which i am using to load xml data into xmlnodeslist and my cXML data.

 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);

Here is my cXML data

<?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>

Can anyone help to do this,

Thanks, Venkat.

1

There are 1 answers

8
Tom Bowen On BEST ANSWER

Try this:

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

For example, this will print out every Credential "domain" and their Identity values:

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

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