0

任何人都可以帮助我使用 Linq 解析以下 XML 吗?我曾尝试使用 Linq,但我可以单独解析每个节点。但我需要将所有节点累积在一个字典中。

<?xml version="1.0" encoding="utf-8" ?>
<purchaseOrder xmlns="http://tempuri.org/po.xsd" orderDate="1999-10-20">
 <shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
 <billTo country="US">
   <name>Robert Smith</name>
   <street>8 Oak Avenue</street>
  <city>Old Town</city>
   <state>PA</state>
  <zip>95819</zip>
  </billTo>
 <comment>Hurry, my lawn is going wild!</comment>
 <items>
<item partNum="872-AA">
  <productName>Lawnmower</productName>
  <quantity>1</quantity>
  <USPrice>148.95</USPrice>
  <comment>Confirm this is electric</comment>
</item>
<item partNum="926-AA">
  <productName>Baby Monitor</productName>
  <quantity>1</quantity>
  <USPrice>39.98</USPrice>
  <shipDate>1999-05-21</shipDate>
</item>
</items>
</purchaseOrder>

提前致谢。

4

2 回答 2

1

尝试这个

XElement doc = XElement.Parse(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
                                <purchaseOrder xmlns=""http://tempuri.org/po.xsd"" orderDate=""1999-10-20"">
                                    <shipTo country=""US"">
                                    <name>Alice Smith</name>
                                    <street>123 Maple Street</street>
                                    <city>Mill Valley</city>
                                    <state>CA</state>
                                    <zip>90952</zip>
                                    </shipTo>
                                        <billTo country=""US"">
                                        <name>Robert Smith</name>
                                        <street>8 Oak Avenue</street>
                                        <city>Old Town</city>
                                        <state>PA</state>
                                        <zip>95819</zip>
                                        </billTo>
                                        <comment>Hurry, my lawn is going wild!</comment>
                                        <items>
                                    <item partNum=""872-AA"">
                                        <productName>Lawnmower</productName>
                                        <quantity>1</quantity>
                                        <USPrice>148.95</USPrice>
                                        <comment>Confirm this is electric</comment>
                                    </item>
                                    <item partNum=""926-AA"">
                                        <productName>Baby Monitor</productName>
                                        <quantity>1</quantity>
                                        <USPrice>39.98</USPrice>
                                        <shipDate>1999-05-21</shipDate>
                                    </item>
                                    </items>
                                </purchaseOrder>");

IEnumerable<XElement> elements = doc.Descendants();//if you like to use elements instead of nodes
foreach (XElement element in elements) {
    Console.WriteLine(String.Format("Name: {0} || Value: {1}",element.Name.LocalName,element.Value));
};
IEnumerable<XNode> nodes = doc.DescendantNodes();//if you like to use nodes instead of elements
foreach (XNode node in nodes)
{
    Console.WriteLine(String.Format("Type: {0} || Value: {1}", node.NodeType.ToString(), node.ToString()));
};
Console.ReadLine();
于 2013-02-09T07:21:02.700 回答
1

您可以创建模型,然后通过DataContractSerializer或反序列化它XmlSerializer

于 2013-02-08T10:27:39.650 回答