无法在XML中找到某个节点

时间:2016-08-23 08:45:13

标签: c# xml xpath

我的问题是我似乎无法获得我需要的节点列表。我尝试了多种解决方案,似乎没有用。这是我的xml文件的一部分:

<findItemsByCategoryResponse xmlns="http://www.ebay.com/marketplace/search/v1/services">
    <ack>Success</ack>
    <version>1.13.0</version>
    <timestamp>2016-08-23T07:33:22.497Z</timestamp>
    <searchResult count="100">
        <item>
            <itemId>152210133431</itemId>
            <title>...</title>
            <globalId>EBAY-GB</globalId>
            <primaryCategory>
                <categoryId>218</categoryId>
                <categoryName>Magic the Gathering</categoryName>
            </primaryCategory>
            <galleryURL>
http://thumbs4.ebaystatic.com/m/meIDrVqhmbpQMYCxzeUvR9Q/140.jpg
</galleryURL>
            <viewItemURL>
http://www.ebay.co.uk/itm/MTG-See-Unwritten-Khans-Tarkir-MYTHIC-MINT-/152210133431
</viewItemURL>
            <paymentMethod>PayPal</paymentMethod>
            <autoPay>false</autoPay>
            <location>London,United Kingdom</location>
            <country>GB</country>
            <shippingInfo>
                <shippingServiceCost currencyId="GBP">1.1</shippingServiceCost>
                <shippingType>Flat</shippingType>
                <shipToLocations>GB</shipToLocations>
            </shippingInfo>
            <sellingStatus>
                <currentPrice currencyId="GBP">0.5</currentPrice>
                <convertedCurrentPrice currencyId="GBP">0.5</convertedCurrentPrice>
                <bidCount>0</bidCount>
                <sellingState>Active</sellingState>
                <timeLeft>P0DT0H19M12S</timeLeft>
            </sellingStatus>
            <listingInfo>
                <bestOfferEnabled>false</bestOfferEnabled>
                <buyItNowAvailable>false</buyItNowAvailable>
                <startTime>2016-08-18T07:52:34.000Z</startTime>
                <endTime>2016-08-23T07:52:34.000Z</endTime>
                <listingType>Auction</listingType>
                <gift>false</gift>
            </listingInfo>
            <condition>
                <conditionId>1000</conditionId>
                <conditionDisplayName>New</conditionDisplayName>
            </condition>
            <isMultiVariationListing>false</isMultiVariationListing>
            <topRatedListing>false</topRatedListing>
        </item>
    </searchResult>
    <paginationOutput>...</paginationOutput>
    <itemSearchURL>...</itemSearchURL>
</findItemsByCategoryResponse>

通常这个xml文件中有100个项目节点,我只是缩短了文件。我试图在XmlNodeList中获取''item''节点和所有内容,但是当我调试它时说它包含0个项目。在此之后,我想检索一些数据,正如您在代码中看到的那样。注意:我尝试了很多xpath值! 这是我使用的代码:

            XmlDocument xmlText = new XmlDocument();
            xmlText.Load(basicUrl);
            XmlElement root = xmlText.DocumentElement;
            XmlNodeList xnList = root.SelectNodes("//item");
            foreach(XmlNode item in xnList)
            {
                string title = item["title"].InnerText;
                Console.WriteLine(title);
            }

1 个答案:

答案 0 :(得分:1)

XPath表达式始终可识别名称空间(如果Element具有名称空间,则XPath必须通过名称空间引用它)。此外,在XPath表达式中,&#39;默认&#39;命名空间总是在URI&#34;&#34;中。在你的情况下,你应该做这样的事情:

Dim i As Long
Dim j As Long
'...
For i = LBound(dat, 1) To UBound(dat, 1)
    For j = LBound(dat, 2) To UBound(dat, 2)
        If dat(i, j) = "3" Or dat(i, j) = "2" Then
            dat(i, j) = ""
        End If
    Next j
Next i

具有命名空间的SelectNodes:

https://msdn.microsoft.com/en-US/library/4bektfx9(v=vs.110).aspx