【问题标题】:How to read sub Nodes from xml nodes list using c#如何使用c#从xml节点列表中读取子节点
【发布时间】:2017-05-26 07:25:09
【问题描述】:

以下是我的 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>

任何人都可以帮忙吗?

谢谢, 文卡特。

【问题讨论】:

  • 您似乎正在读取 for 循环中的所有子节点。您究竟想对您目前无法使用的子节点做什么?
  • 例如 在这我需要时间戳值和 payloadId 值.. 就像我需要来自 cxml 的所有值?所以我是怎么知道的,你能解释一下吗......!在此先感谢,文卡特
  • XmlNodeList nodes = doc.DocumentElement.SelectNodes("//Credential");你能建议我上面的方法可以读取节点吗?

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


【解决方案1】:

试试这个:

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 + ",");              
}

【讨论】:

  • foreach(节点中的 XmlNode 节点){ yourString.Append(node["Name"].InnerText + ","); } 这里的节点没有得到任何东西,指针没有进入 foreach 块
  • 首先你必须找到你想要的确切节点。然后运行我的答案中的代码。
  • XmlNodeList nodes = doc.DocumentElement.SelectNodes("//Credential");你能建议我上面的方法可以读取节点吗?
  • 现在我正在获取凭据下的域值 FakeIdentityFakeIdentity1 但要获取身份值它不起作用
  • 我已经更新了我的答案,包括获取凭据下的身份节点的值。您需要获取的任何其他详细信息都可以通过重复使用我给您的示例来完成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多