How do I display an xml file properly?

I’m pretty new to this so I’m a little helpless when it comes to solving an issue such as this. I’m trying to figure out how can I display a file order.xml properly. 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 program code, the method DisplayOrder() in program.cs that is trying to list out 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... rn");
        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);
    }
}        

Therefore, my main problem is that I am unsure of how I am able change the outcome of the output in the console, as seen 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 I would like them to. Help would be greatly appreciated. Thank you.


Source: xml

Leave a Reply