【问题标题】:Windows phone 7 xml linq queries troubleWindows phone 7 xml linq 查询麻烦
【发布时间】:2011-06-29 16:53:10
【问题描述】:

大家好,我正在为 windows phone 7 开发应用程序,但在使用 XDocument 和 Linq 查询解析 XML 文档时遇到了一些问题。我只想要一段相当大的 XML 的特定部分,并且节点上有很多重复的名称。

<itemListCollection xmlns="urn:webjet.com.au" xmlns:i="http://www.w3.org/2001/XMLSchema-?Instance">
<itemList>
    <listCode>FlightSearchAirportsTT</listCode>
    <items>
        <item>
            <code>ADL</code>
            <value>Adelaide</value>
        </item>
        <item>
            <code>BNE</code>
            <value>Brisbane</value>
        </item>
         ....
         ....
    </items>
</itemList>
<itemList>
    <listCode>AIRPORTCODES_LATLONS</listCode>
    <items>
        <item>
            <code>EBB</code>
            <value>0.060277777777778;32.4</value>
        </item>
        <item>
            <code>NYK</code>
            <value>0.066666666666667;37.03333333333</value>
        </item>
                    ........
                    .......
  <item>
            <code>KAB</code>
            <value>ZW</value>
        </item>
        <item>
            <code>VFA</code>
            <value>ZW</value>
        </item>
    </items>
</itemList>

我只想要第一个 itemList 中的项目,因此我可以使用它们创建一个具有两个属性机场代码和机场名称的对象。但是我不知道如何提取那个确切的部分。

【问题讨论】:

    标签: xml linq windows-phone-7


    【解决方案1】:

    这将返回名为 itemList 的第一个 XElement。

            // if your xml is in a file:
            XDocument doc = XDocument.Load("/path/of/xml");
    
            // if your xml is in a string field:
            XDocument doc = XDocument.Parse(stringField);
            XElement firstList = doc.Descendants().Where(x => x.Name == "itemList").First();
    

    【讨论】:

    • 感谢 valipour。对于其他想知道我然后使用 foreach (XElement element in firstList.Descendants(ns+"item")) { airportElements.Add(element); } 单独获取
    猜你喜欢
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2011-12-09
    相关资源
    最近更新 更多