gpt4 book ai didi

c# - 如何正确显示 xml 文件?

转载 作者:数据小太阳 更新时间:2023-10-29 02:44:41 26 4
gpt4 key购买 nike

我对此很陌生,所以在解决此类问题时我有点无能为力。我想弄清楚如何才能正确显示文件 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>

这是我的程序代码,program.cs 中的方法 DisplayOrder() 试图列出控制台:

    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

最佳答案

对您希望如何输出它做出一些假设,并使用 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);
}

关于c# - 如何正确显示 xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30391992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com