0
<?xml version="1.0"?>
<!--
  Created by Wong Kai Chong
  BSPI 2017
  Temasek Polytechnic
-->

<order>
  <!-- Simple Type - Main Data -->
  <invoiceid>0011995</invoiceid>
  <invoicedate>12/5/2017</invoicedate>
  <sellerid>0020</sellerid>
  <buyerid>1231</buyerid>
  <orderid>9021</orderid>

  <!-- An Item - Multiple -->
  <item>
    <itemid>0001</itemid>
    <itemname>Apple Macbook Pro</itemname>
    <description>The best professional laptop for professionals.</description>
    <quantity>5</quantity>
    <newunitprice>4950.50</newunitprice>
  </item>

  <!-- An Item - Multiple -->
  <item>
    <itemid>0002</itemid>
    <itemname>Microsoft Surface Laptop</itemname>
    <description>The most versatile professional laptop for experts.</description>
    <quantity>10</quantity>
    <newunitprice>3500.90</newunitprice>
  </item>

  <!-- An Item - Multiple -->
  <item>
    <itemid>0003</itemid>
    <itemname>Apple iPhone</itemname>
    <description>The best consumer mobile phone.</description>
    <quantity>1000</quantity>
    <newunitprice>500.00</newunitprice>
  </item>

  <!-- An Item - Multiple -->
  <item>
    <itemid>0004</itemid>
    <itemname>Samsung Galaxy S8</itemname>
    <description>The most amazing screen on a phone.</description>
    <quantity>3000</quantity>
    <newunitprice>550.00</newunitprice>
  </item>

  <!-- Simple Type - Footer Data -->
  <shippingcharges>7000.00</shippingcharges>
  <invoicetotalcost>19500.10</invoicetotalcost>
</order>

And at the bottom here is the Visual Studio C# codes.

        string url = "../../../invoiceData.xml";
        XmlReader reader = XmlReader.Create(url);
        StringBuilder orderList = new StringBuilder();
        orderList.Append("Order List: ").Append(Environment.NewLine);
        int counter = 0;
        while (reader.ReadToFollowing("item"))
        {
            counter++;
            orderList.Append("Invoice counter: " + counter + Environment.NewLine);
            reader.ReadToFollowing("invoiceID");
            orderList.Append("Invoice ID: " + reader.ReadElementContentAsString() + Environment.NewLine);

            reader.ReadToFollowing("invoiceid");
            orderList.Append("Invoice ID: " + reader.ReadElementContentAsString());

            reader.ReadToFollowing("invoicedate");
            orderList.Append("Invoice Date: " + reader.ReadElementContentAsString());
        }``

This will be for my homework. So you will be helping a lot. The problem here is I don't know if my invoiceData.xml is even correct. And if it is, how do I write the codes to read them?

    string url = "../../invoice1.xml"; // assume args[0] is invoice1.xml
    XmlReader reader = XmlReader.Create(url);
    StringBuilder invoiceList = new StringBuilder();
    invoiceList.Append("Order List:").Append(Environment.NewLine);
    int counter = 0;
    while (reader.ReadToFollowing("order")) //read to each order element
    {
        counter++; //increase the counter
        invoiceList.Append("Order Counter: " + counter + Environment.NewLine);
        reader.ReadToFollowing("orderID"); //move to orderID element
        invoiceList.Append("Order ID: " + reader.ReadElementContentAsString() + Environment.NewLine); // read OrderID element content

        reader.ReadToFollowing("item"); //move to item element
        int itemCount = 1;
        invoiceList.Append("item: "+itemCount + reader.ReadElementContentAsString() + Environment.NewLine); // read item element content

        while (reader.ReadToNextSibling("item")) //move to next item element
        {
            itemCount++;
            invoiceList.Append("item: "+itemCount + reader.ReadElementContentAsString() + Environment.NewLine); // read item element content
        }

        reader.ReadEndElement();

This 3rd segment of codes are given to me as an example. They work as a boilerplate for me to modify and personalise to fit my solution. If you could, are you able to explain it in a way where I can understand these codes in order to personify for myself?

Best, KAI

DrWongKC
  • 139
  • 7
  • You don't have to give me an answer, telling me how the reading of XML works would be great too! – DrWongKC Jun 16 '17 at 13:17
  • 1
    Exactly what is your problem - other than not knowing if the XML is correct? Do you get an error when you run your code - if so, what is the error you get. If the code runs but gives unexpected results - then let us know what you expect & what you get. – PaulF Jun 16 '17 at 13:19
  • You can use a website like this to validate your XML - http://www.xmlvalidation.com/ - plenty of others if you use Google. – PaulF Jun 16 '17 at 13:23
  • Also Google or search here for parsing XML - you will get lots of results like : https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c & https://stackoverflow.com/questions/55828/how-does-one-parse-xml-files – PaulF Jun 16 '17 at 13:28
  • The XML is correct. – dbasnett Jun 16 '17 at 13:30
  • You do not read XML in Visual Studio, but with your program language (i.e. C#/.NET). VS is just your development environment (IDE) for being able to do so. – CHS Jun 16 '17 at 13:36
  • Hi there! Thank you so much for the replies. I have added a comment in the end of that question, could you read it and let me know? :D – DrWongKC Jun 16 '17 at 13:42
  • Oh yes when I say personify, I mean being able to weld it and use it with my invoiceData.xml Thank you for not scolding me for asking such a stupid question. – DrWongKC Jun 16 '17 at 13:44

1 Answers1

0

Teacher probably want you to use serializer class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string INPUT_FILENAME = @"c:\temp\test.xml";
        const string OUTPUT_FILENAME = @"c:\temp\test1.xml";
        static void Main(string[] args)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(Order));
            XmlTextReader reader = new XmlTextReader(INPUT_FILENAME);

            Order order = (Order)deserializer.Deserialize(reader);


            XmlSerializer serializer = new XmlSerializer(typeof(Order));

            StreamWriter writer = new StreamWriter(OUTPUT_FILENAME);
            serializer.Serialize(writer, order);
            writer.Flush();
            writer.Close();
            writer.Dispose();
        }
    }

    [XmlRoot("order")]
    public class Order
    {
        public int invoiceid { get; set; }
        private DateTime _invoicedate { get; set; }

        public string invoicedate
        {
            get { return _invoicedate.ToString("d/m/yyyy"); }
            set { _invoicedate = DateTime.ParseExact(value, "d/m/yyyy", CultureInfo.InvariantCulture); }
        }

        public int sellerid { get; set; }
        public int buyerid { get; set; }
        public int orderid { get; set; }

        [XmlElement("item")]
        public List<Item> items { get; set; }

        public decimal shippingcharges { get; set; }
        public decimal invoicetotalcost { get; set; }

    }
    [XmlRoot("item")]
    public class Item
    {
        public int itemid { get; set; }
        public string itemname { get; set; }
        public string description { get; set; }
        public int quantity { get; set; }
        public decimal newunitprice { get; set; }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • This is great! My teacher has approved these codes so I can use it. Thank you so much! – DrWongKC Jun 18 '17 at 15:16
  • There are many ways of readling xml. In your case I felt the code I posted was very simple and would make it easy to read and modify the data. Also data can easily be written back to a file with a few added instructions. – jdweng Jun 18 '17 at 15:39
  • Thanks for replying, I would like to write back the data using the stream method. How do I do that? – DrWongKC Jun 18 '17 at 15:53
  • Updated my code. – jdweng Jun 18 '17 at 16:40