I have an xml file like this:
<?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. I'm using XmlTextReader to parse through the XML file. Update with the full code below.
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);
}
}
}
}
}
}```
With the code below, I'm trying to get just the tax amount but keep extracting the "Principal" amount too. I'll attach a photo for reference.
How do I just show the Tax amount? Thanks for any help to a newbie!
Not sure what is the problem but Alexander in the above comments has already given a solution for this. The link of the previous post (Reading xml child tag value).
I just combined the 2 programs and done a small alteration so that you can get discretely labeled both the Tax amount & the Principal amount. This would look like:
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);
}
}
}
You will see that the end result is:
City =SAN JOSE
Principal Amount =14.98
Tax Amount =1.39