Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

c# - Reading xml child tag value

My xml file looks like below.

<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
 <DocumentVersion>1.00</DocumentVersion>
 </Header>
<MessageType>AllOrdersReport</MessageType>
<Message>
     <Order>
      <OrderID>14-7005-04201</OrderID>
      <MerchantOrderID>14-7005-04201</MerchantOrderID>
      <PurchaseDate>2020-07-28T02:32:38+00:00</PurchaseDate>
      <LastUpdatedDate>2020-07-28T14:58:07+00:00</LastUpdatedDate>
      <OrderStatus>Shipped</OrderStatus>
      <SalesChannel>Amazon.com</SalesChannel>
      <FulfillmentData>
         <FulfillmentChannel>Amazon</FulfillmentChannel>
         <ShipServiceLevel>Expedited</ShipServiceLevel>
         <Address>
            <City>SAN JOSE</City>
            <State>CA</State>
            <PostalCode>95129-3137</PostalCode>
            <Country>US</Country>
         </Address>
     </FulfillmentData>
     <IsBusinessOrder>false</IsBusinessOrder>
     <OrderItem>
        <AmazonOrderItemCode>4494901738</AmazonOrderItemCode>
        <ASIN>B0N1QG</ASIN>
        <SKU>CH-4219</SKU>
        <ItemStatus>Shipped</ItemStatus>
        <ProductName>Drawing Pad</ProductName>
        <Quantity>1</Quantity>
        <ItemPrice>
           <Component>
              <Type>Principal</Type>
              <Amount currency="USD">14.98</Amount>
           </Component>
           <Component>
              <Type>Tax</Type>
              <Amount currency="USD">1.39</Amount>
           </Component>
        </ItemPrice>
     </OrderItem>
  </Order>
</Message>
</AmazonEnvelope>

C# code to read the value of each tag.



while (xtr.Read())
          {
            if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "AmazonOrderID")
            {
              string s1 = xtr.ReadElementContentAsString();
              Console.WriteLine("AmazonOrderID ="+s1);
            }
            if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "PurchaseDate")
            {
              string s2 = xtr.ReadElementContentAsString();
              Console.WriteLine("PurchaseDate ="+s2);
            }
            
            if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "State")
            {
              string s3 = xtr.ReadElementContentAsString();
              Console.WriteLine("State ="+s3);
            }
            if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "ProductName")
            {
              string s4 = xtr.ReadElementContentAsString();
              Console.WriteLine("ProductName ="+s4);
            }
            if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "Quantity")
            {
              string s5 = xtr.ReadElementContentAsString();
              Console.WriteLine("Quantity ="+s5);
            }
            if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "PostalCode")
            {
              string s6 = xtr.ReadElementContentAsString();
              Console.WriteLine("PostalCode ="+s6);
            }
            if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "City")
            {
              string s7 = xtr.ReadElementContentAsString();
              Console.WriteLine("City ="+s7);
            }
             if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "Tax")
            {
              string s8 = xtr.ReadElementContentAsString();
              Console.WriteLine("Tax ="+s8);
            }
          
            
          }
        }    
    }
}

With the above code, I am able to iterate through all the tag elements. But I am not able to read "Tax" tag value. How do I read the value of "Tax" tag to get the $1.39 amount?

Thanks for any help in figuring out how to iterate through the "Tax" line.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"Tax" is not element name. It is value of "Type" node.

So you have to find "Type" node with "Tax" value. Then find "Amount" and read it.

if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "Type")
{
    string s8 = xtr.ReadElementContentAsString();
    if (s8 == "Tax")
    {
        xtr.ReadToFollowing("Amount");
        decimal amount = xtr.ReadElementContentAsDecimal();
        Console.WriteLine("Tax =" + amount);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...