简体   繁体   English

找不到XML中的某个节点

[英]Can't find a certain node in XML

My problem is that I can't seem to get a list of nodes I need. 我的问题是我似乎无法获得所需的节点列表。 I have tried multiple solutions and it doesn't seem to work. 我尝试了多种解决方案,但似乎没有用。 This is a part of my xml file: 这是我的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>

Normally there are 100 item nodes in this xml file, I just shorted the file. 通常,在这个xml文件中有100个项目节点,我只是将该文件短路了。 I tried to get the ''item'' nodes and everything inside in a XmlNodeList, but when I debug it says it contains 0 items. 我试图在XmlNodeList中获取“ item”节点和内部的所有内容,但是当我调试时说它包含0个项目。 After this I want to retrieve some data, as you can see in the code. 在此之后,我想检索一些数据,如您在代码中所见。 Note: I tried a lot of xpath values! 注意:我尝试了很多xpath值! Here is the code I used: 这是我使用的代码:

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

XPath Expressions are always namespace aware (if the Element has a Namespace then the XPath must reference it by namespace). XPath表达式始终支持名称空间(如果Element具有名称空间,则XPath必须按名称空间引用它)。 Also, in XPath expressions, the 'default' namespace is always in the URI "". 另外,在XPath表达式中,“默认”名称空间始终位于URI“”中。 In your case you should do something like this : 在您的情况下,您应该执行以下操作:

        XmlDocument xmlText = new XmlDocument();
        xmlText.Load(yourXml);
        XmlElement root = xmlText.DocumentElement;
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlText.NameTable);
        nsmgr.AddNamespace("t", "http://www.ebay.com/marketplace/search/v1/services");
        XmlNodeList xnList = xmlText.SelectNodes("//t:item", nsmgr);
        foreach (XmlNode item in xnList)
        {
            string title = item["title"].InnerText;
            Console.WriteLine(title);
        }

SelectNodes with namespace : 具有名称空间的SelectNodes:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM