我对此很陌生,所以在解决像这样的问题时我有点无助。我正在试图找出如何正确显示文件 order.xml
。我在C#编码。
这是XML文件:
<?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>
这是我的程序代码, DisplayOrder()
中的方法program.cs
试图在控制台中列出订单:
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);
}
}
因此,我的主要问题是我不确定如何在控制台中更改输出的结果,如下所示:
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
“商品”未按照我的意愿列出。非常感谢帮助。谢谢。
预期输出
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
答案 0 :(得分:0)
对您希望如何输出这些内容进行一些假设,并使用LINQ to XML,因为它通常比旧XmlDocument
API更好的API:
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);
}
答案 1 :(得分:0)
您的意思是您不希望显示整个<item ....
内容?这来自行list.Append(childNode.InnerText) ...
,没有任何案例标签匹配。尝试:
default:
continue;
希望它实际上跳过for
循环的迭代..