【问题标题】:Parsing Complex xml in windows phone7在 windows phone7 中解析复杂的 xml
【发布时间】:2013-01-24 01:45:43
【问题描述】:

谁能帮我用 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>

提前致谢。

【问题讨论】:

    标签: c# windows-phone-7 xml-parsing linq-to-xml


    【解决方案1】:

    您可以创建模型,然后通过DataContractSerializerXmlSerializer 对其进行反序列化

    【讨论】:

    • k.但我需要通过 Linq 查询来实现这一点。你能帮忙吗?
    【解决方案2】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多