如何使用c#从xml节点列表中读取子节点

时间:2017-05-26 07:25:10

标签: c# asp.net-web-api xml-serialization cxml

以下是我的cXML数据,并且能够从xml有效负载请求将cxml数据加载到xmlnodeslist,现在我想从xmlnodeslist中读取所有子节点以下是用于将xml数据加载到xmlnodeslist和我的cXML中的代码数据

 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">fake@bn.com</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">fake@bn.com</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">fake@bn.com</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。

1 个答案:

答案 0 :(得分:0)

试试这个:

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

例如,这将打印出每个凭据&#34;域&#34;和他们的身份值:

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

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