读取 xml 个子标签值
Reading xml child tag value
我的 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# 代码。
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);
}
}
}
}
}
通过上面的代码,我可以遍历所有的标签元素。但我无法读取“税”标签值。如何读取“税”标签的值以获得 1.39 美元的金额?
感谢您在确定如何遍历“税收”行方面提供的任何帮助。
“税”不是元素名称。它是“类型”节点的值。
因此您必须找到具有“税”值的“类型”节点。然后找到“金额”并阅读它。
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);
}
}
我的 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# 代码。
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);
}
}
}
}
}
通过上面的代码,我可以遍历所有的标签元素。但我无法读取“税”标签值。如何读取“税”标签的值以获得 1.39 美元的金额?
感谢您在确定如何遍历“税收”行方面提供的任何帮助。
“税”不是元素名称。它是“类型”节点的值。
因此您必须找到具有“税”值的“类型”节点。然后找到“金额”并阅读它。
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);
}
}