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