How to display XML file correctly?
I'm new to this, so I'm a bit helpless when it comes to solving a problem like this. I am trying to figure out how to display the file correctly order.xml
. I am coding in C #.
This is the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Order>
<Items orderId="4001">
<orderDate>22/05/2015</orderDate>
<buyerId>85852020</buyerId>
<item itemId="8002">
<itemName>LCD Screen</itemName>
<desc>Component used for computer displays</desc>
<quantity>20</quantity>
<unitPrice>110.40</unitPrice>
</item>
<item itemId="8010">
<itemName>Cooling Fan</itemName>
<desc>Cools computer components</desc>
<quantity>50</quantity>
<unitPrice>65.50</unitPrice>
</item>
<item itemId= "8030">
<itemName>Keyboard</itemName>
<desc>Facilitates text input into computer</desc>
<quantity>50</quantity>
<unitPrice>25.90</unitPrice>
</item>
</Items>
and this is my code, method DisplayOrder()
in program.cs
, that tries to display the order in the console:
private static void DisplayOrder()
{
// Get a list of all XmlNodes with the tag Contact
XmlNodeList orderList = orderDoc.GetElementsByTagName("Items");
Console.WriteLine("Displaying the Orders... \r\n");
StringBuilder list = new StringBuilder();
foreach (XmlNode node in orderList)
{
list.Append(Environment.NewLine);
if (node.Attributes.Count > 0)
list.Append("Order ID = ").Append(
node.Attributes[0].Value).Append(Environment.NewLine);
if (node.HasChildNodes)
{
XmlElement childNode = (XmlElement)node.FirstChild;
for (int idx = 0; idx < node.ChildNodes.Count; idx++)
{
switch (childNode.Name)
{
//case "orderId":
// list.Append("Order ID = ");
// break;
case "orderDate":
list.Append("Order Date = ");
break;
case "buyerId":
list.Append("Buyer ID = ");
break;
case "itemId":
list.Append("Item ID = ");
break;
case "itemName":
list.Append("Item Name = ");
break;
case "desc":
list.Append("Description = ");
break;
case"quantity":
list.Append("Quantity = ");
break;
case "unitPrice":
list.Append("Existing Unit Price = ");
break;
default:
break;
}
list.Append(childNode.InnerText).Append(Environment.NewLine);
childNode = (XmlElement)childNode.NextSibling;
}
}
list.Append(Environment.NewLine);
}
Console.WriteLine(list);
}
}
So my main problem is that I am not sure how I can change the output in the console as shown below:
Displaying the Orders...
Order ID = 4001
Order Date = 22/05/2015
Buyer ID = 85852020
LCD ScreenComponent used for computer displays20110.40
Cooling FanCools computer components5065.50
KeyboardFacilitates text input into computer5025.90
The "items" are not listed as desired. Help would be greatly appreciated. Thank.
Expected Result:
Displaying the Orders...
Order ID = 4001
Order Date = 22/05/2015
Buyer ID = 85852020
Item Name = LCD Screen
Description = Component used for computer displays
Quantity = 20
Price = 110.40
Item Name = Cooling Fan
Description = Cools computer components
Quantity = 50
Price = 65.50
Item Name = Keyboard
Description = Facilitates text input into computer
Quantity = 50
Price = 25.90
source to share
Let's make some assumptions about how you want this to be output, and using LINQ to XML, since it's usually a more convenient API than the old API XmlDocument
:
var doc = XDocument.Parse(xml);
var orderId = (string)doc.Descendants("Items")
.Select(e => e.Attribute("orderId"))
.Single();
var orderDate = (string)doc.Descendants("orderDate").Single();
var buyerId = (string)doc.Descendants("buyerId").Single();
Console.WriteLine("Order ID: {0}", orderId);
Console.WriteLine("Order Date: {0}", orderDate);
Console.WriteLine("Buyer ID: {0}", buyerId);
foreach (var item in doc.Descendants("item"))
{
var id = (string)item.Attribute("itemId");
var name = (string)item.Element("itemName");
var description = (string)item.Element("desc");
var quantity = (int)item.Element("quantity");
var price = (decimal)item.Element("unitPrice");
Console.WriteLine("---");
Console.WriteLine("Item ID: {0}", id);
Console.WriteLine("Item Name: {0}", name);
Console.WriteLine("Item Description: {0}", description);
Console.WriteLine("Item Quantity: {0}", quantity);
Console.WriteLine("Item Price: {0}", price);
}
source to share