读取xml文件的NodeType值

史蒂夫

我有一个像这样的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-输出

Efthymios Kalyviotis

不确定是什么问题,但是亚历山大在上面的评论中已经给出了解决方案。上一篇文章的链接(阅读xml子标记值)。

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章