Xml,C#,XmlNodeList没有获得选定的节点

时间:2016-06-09 09:24:53

标签: c# xml parsing xpath xmlnodelist

我是编程新手,不明白,为什么这段代码:

XmlNodeList elemPrice = doc.SelectNodes(
    "//Items/Item/Offers/Offer/OfferListing/Price/FormattedPrice");

不要给选定的节点。我看了xml doc获取xpath,它是对的。

在searchItem.Price行中,当尝试执行此操作时,它为null:

List <SearchedItem> searchedItems = new List<SearchedItem>();

for (int i = 0; i < elemAsin.Count; i++)
{
    SearchedItem searchedItem = new SearchedItem();
    searchedItem.Asin = elemAsin[i].InnerText;
    searchedItem.CurrencyCode = elemCurrencyCode[i].InnerText;
    string price = elemPrice[i].InnerText.Remove(0, 1);
    searchedItem.Price = double.Parse(price);
    searchedItems.Add(searchedItem);
}

return searchedItems;

2 个答案:

答案 0 :(得分:0)

提供一个使用Xpath选择节点的简单示例,尝试这种方式

        XmlDocument doc = new XmlDocument();

        doc.Load(@"c:\XMLFile.xml");

        XmlNodeList elemPrice = doc.SelectNodes("/catalog/cd/price");

        for (int i = 0; i < elemPrice.Count; i++)
        {
            Console.WriteLine(elemPrice[i].Name);
            Console.WriteLine(elemPrice[i].InnerText);
        }

&#13;
&#13;
<?xml version="1.0" encoding="utf-8" ?> 
<catalog>
  <cd country="USA">
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <price>10.90</price>
  </cd>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>10.0</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <price>9.90</price>
  </cd>
</catalog>
&#13;
&#13;
&#13;

输出 价钱 10.90 价钱 10.0 价钱 9.90

答案 1 :(得分:0)

我找到了解析XML的其他方法,这很简单。

 public static List<SearchedItem> ItemSearchResponse(string url)
    {
        List<SearchedItem> searchedItems = new List<SearchedItem>();

        WebRequest request = HttpWebRequest.Create(url);
        HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

        XmlDocument doc = new XmlDocument();

        StreamReader reader = new StreamReader(webResponse.GetResponseStream());

        doc.LoadXml(reader.ReadToEnd());

        XmlNodeList listIsValid = doc.GetElementsByTagName("IsValid");

        if (listIsValid.Count > 0 && listIsValid[0].InnerXml == "True")
        {
            XmlNodeList listItems = doc.GetElementsByTagName("Item");

            foreach (XmlNode nodeItem in listItems)
            {
                SearchedItem searchedItem = new SearchedItem();

                foreach (XmlNode nodeChild in nodeItem.ChildNodes)
                {
                    if (nodeChild.Name == "DetailPageURL")
                    {
                        searchedItem.DetailPageURL = nodeChild.InnerText;
                    }

                    else if (nodeChild.Name == "SmallImage")
                    {
                        foreach (XmlNode nodeURLImg in nodeChild.ChildNodes)
                        {
                            if (nodeURLImg.Name == "URL")
                            {
                                searchedItem.SmallImage = nodeURLImg.InnerText;
                            }
                        }
                    }

                    else if (nodeChild.Name == "ItemAttributes")
                    {
                        foreach (XmlNode nodeItemAtributes in nodeChild.ChildNodes)
                        {
                            if (nodeItemAtributes.Name == "Title")
                            {
                                searchedItem.Title = nodeItemAtributes.InnerText;
                            }
                        }
                    }

                    else if (nodeChild.Name == "OfferSummary")
                    {
                        foreach (XmlNode nodeOfferSummary in nodeChild.ChildNodes)
                        {
                            if (nodeOfferSummary.Name == "LowestNewPrice")
                            {
                                foreach (XmlNode nodeLowestNewPrice in nodeOfferSummary.ChildNodes)
                                {
                                    if (nodeLowestNewPrice.Name == "CurrencyCode")
                                    {
                                        searchedItem.CurrencyCode = nodeLowestNewPrice.InnerText;
                                    }

                                    else if (nodeLowestNewPrice.Name == "FormattedPrice")
                                    {
                                        string price = nodeLowestNewPrice.InnerText.Remove(0, 1);
                                        searchedItem.Price = Double.Parse(price);
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    else if (nodeChild.Name == "CustomerReviews")
                    {
                        foreach (XmlNode nodeCustomerReview in nodeChild.ChildNodes)
                        {
                            if (nodeCustomerReview.Name == "Review")
                            {
                                foreach (XmlNode nodeReview in nodeCustomerReview.ChildNodes)
                                {
                                    if (nodeReview.Name == "Content")
                                    {
                                        searchedItem.CustomerReview = nodeReview.InnerText;
                                    }
                                }
                            }
                        }
                    }
                }
                searchedItems.Add(searchedItem);
            }
        }
                    return searchedItems;
    }

谢谢你的帮助!