读取 xml 文件的 NodeType 值

Reading NodeType Value of xml file

我有一个这样的 xml 文件:

<?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# 代码。我正在使用 XmlTextReader 来解析 XML 文件。使用下面的完整代码更新。

using System;
using System.Xml;

namespace democonsole
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlTextReader xtr = new XmlTextReader("");

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

          }
        }
    }
}```


使用下面的代码,我试图只获取税额,但也继续提取“本金”金额。我会附上照片以供参考。

如何只显示税额?感谢您对新手的任何帮助!

VSC -Output

不确定是什么问题,但亚历山大在上面的评论中已经给出了解决方案。前一个post的link().

我只是合并了这两个程序并做了一个小改动,这样您就可以分别标记税额和本金金额。这看起来像:

XmlTextReader xtr = new XmlTextReader("xml.txt");

while (xtr.Read())
{

    if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "City")
    {
        string s7 = xtr.ReadElementContentAsString();
        Console.WriteLine("City =" + s7);
    }

    if (xtr.NodeType == XmlNodeType.Element && xtr.Name == "Component")
    {
        xtr.ReadToFollowing("Type");

        string s8 = xtr.ReadElementContentAsString();
        if (s8 == "Tax")
        {
            xtr.ReadToFollowing("Amount");
            decimal amount = xtr.ReadElementContentAsDecimal();
            Console.WriteLine("Tax Amount =" + amount);
        }
        else if (s8 == "Principal")
        {
            xtr.ReadToFollowing("Amount");
            decimal amount = xtr.ReadElementContentAsDecimal();
            Console.WriteLine("Principal Amount =" + amount);
        }
    }
}

你会看到最终结果是:

City =SAN JOSE
Principal Amount =14.98
Tax Amount =1.39