2017-06-16 35 views
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> 

而在這裏的底部是Visual Studio C#代碼。如何使用DOM方法在Visual Studio中讀取XML?我的invoiceData.xml正確嗎?

 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()); 
     }`` 

這將是我的作業。所以你會幫助很多。這裏的問題是我不知道我的invoiceData.xml是否正確。如果是這樣,我該如何編寫代碼來閱讀它們?

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(); 

這段代碼的第三部分給我作爲例子。他們作爲我的樣板進行修改和個性化以適應我的解決方案。如果可以的話,你能以我能理解這些代碼的方式來解釋它嗎?

最佳, KAI

+0

你不必給我一個答案,告訴我如何將XML作品的閱讀將是巨大的! – DrWongKC

+1

您的問題到底是什麼 - 除了不知道XML是否正確?當你運行你的代碼時你會得到一個錯誤 - 如果是這樣,你得到的錯誤是什麼。如果代碼運行但會產生意想不到的結果 - 那麼讓我們知道您的期望和您得到的結果。 – PaulF

+0

你可以使用這樣的網站來驗證你的XML - http://www.xmlvalidation.com/ - 如果你使用Google,還有很多其他的。 – PaulF

回答

0

教師可能需要您使用串行類

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; } 
    } 

} 
+0

這太棒了!我的老師批准了這些代碼,所以我可以使用它。非常感謝! – DrWongKC

+0

讀取xml有很多種方法。在你的情況下,我覺得我發佈的代碼非常簡單,並且可以很容易地讀取和修改數據。還可以通過添加幾條指令輕鬆將數據寫回文件。 – jdweng

+0

感謝您的回覆,我想使用流方法回寫數據。我怎麼做? – DrWongKC

相關問題