<?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
你不必給我一個答案,告訴我如何將XML作品的閱讀將是巨大的! – DrWongKC
您的問題到底是什麼 - 除了不知道XML是否正確?當你運行你的代碼時你會得到一個錯誤 - 如果是這樣,你得到的錯誤是什麼。如果代碼運行但會產生意想不到的結果 - 那麼讓我們知道您的期望和您得到的結果。 – PaulF
你可以使用這樣的網站來驗證你的XML - http://www.xmlvalidation.com/ - 如果你使用Google,還有很多其他的。 – PaulF