Xml、C#、XmlNodeList 不会获取选定的节点



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

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

不要提供选定的节点。我看了 xml 文档以获取 xpath,它是正确的。

当尝试执行以下命令时,它在searchedItem.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;

提供一个使用 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);
        }

<?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>

输出价格10.90价格10.0价格9.90

我找到了其他方法来解析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;
    }

谢谢你的帮助!

相关内容

  • 没有找到相关文章

最新更新