抓住子元素的xml子元素

时间:2017-10-22 12:28:21

标签: c# asp.net-mvc

我已成功访问项目集合下的itemId,galleryURL,title,viewItemURL值,但问题是我需要访问另一个值,该值不直接位于另一个子项内的项集合下。请查看XML picture of XML的这张图片,以便更好地了解。请告诉我如何访问listingInfo-> watchCount

public ActionResult Search(string OperationName, string calltype, string page, string keywords, string type, string location, string condition, string min, string max, string negative, string minFeedback, string maxFeedback, string drange, string categoryId)
{
    string AppId = "demo-key"; //api configs
    string BaseUrl = "http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME="; //base url api end point

    if (calltype == "categoryClick")
    {
        string Url = BaseUrl + OperationName + "&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=" + AppId + "&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&categoryId=" + categoryId + "&paginationInput.entriesPerPage=2&paginationInput.pageNumber=" + page + "";

        var items = new List<EbayDataViewModel>();

        XDocument xdoc = XDocument.Load(Url);

        // Since i am only interested in <item> collections within <searchResult>
        var searchResultItems = xdoc.Descendants()
            .Where(x => x.Name.LocalName == "item");

        foreach (var sri in searchResultItems)
        {
            // Get all child xml elements
            var childElements = sri.Elements();

            var itemId = childElements.FirstOrDefault(x => x.Name.LocalName == "itemId");
            var imageurl = childElements.FirstOrDefault(x => x.Name.LocalName == "galleryURL");
            var title = childElements.FirstOrDefault(x => x.Name.LocalName == "title");
            var url = childElements.FirstOrDefault(x => x.Name.LocalName == "viewItemURL");
           // var numberofwatch = childElements.Where(x => x.Name.LocalName == "listinginfo"); this is one step inside of another element

            //add items from xml data to EbayDataViewModel object
            items.Add(new EbayDataViewModel
            {
                ItemId = itemId == null ? String.Empty : itemId.Value,
                EbayImageUrl = imageurl == null ? String.Empty : imageurl.Value,
                EbayTitle = title == null ? String.Empty : title.Value,
                EbayUrl = url == null ? String.Empty : url.Value,
                //NumberOfWatch = numberofwatch == null ? String.Empty : numberofwatch.Value,
            });
        }
        var e = Json(items);
        return Json(items);

    }else{

    }

    return null;
}

2 个答案:

答案 0 :(得分:0)

您可以使用XPathSelectElement(请参阅MSDN XPathSelectElement

string str =
@"<?xml version=""1.0""?>
<sri>
    <item><listingInfo><watchCount>1</watchCount></listingInfo></item>
    <item><listingInfo><watchCount>2</watchCount></listingInfo></item>
</sri>";

XDocument xdoc = XDocument.Parse(str);
var searchResultItems = xdoc.Descendants().Where(x => x.Name.LocalName == "item");
foreach (var item in searchResultItems)
{
    var wc = item.XPathSelectElement("listingInfo/watchCount");
    Console.WriteLine(wc.Value);
}

答案 1 :(得分:0)

这行代码解决了我认为的问题

  

var nofwatch = childElements.FirstOrDefault(x =&gt; x.Name.LocalName ==   &#34; listingInfo&#34;)。Elements()。FirstOrDefault(x =&gt; x.Name.LocalName ==   &#34; watchCount&#34);

foreach (var sri in searchResultItems)
                {
                    // Get all child xml elements
                    var childElements = sri.Elements();

                    var itemId = childElements.FirstOrDefault(x => x.Name.LocalName == "itemId");
                    var imageurl = childElements.FirstOrDefault(x => x.Name.LocalName == "galleryURL");
                    var title = childElements.FirstOrDefault(x => x.Name.LocalName == "title");
                    var url = childElements.FirstOrDefault(x => x.Name.LocalName == "viewItemURL");
                    var nofwatch = childElements.FirstOrDefault(x => x.Name.LocalName == "listingInfo").Elements().FirstOrDefault(x => x.Name.LocalName == "watchCount");

                    //add items from xml data to EbayDataViewModel object
                    items.Add(new EbayDataViewModel
                    {
                        ItemId = itemId == null ? String.Empty : itemId.Value,
                        EbayImageUrl = imageurl == null ? String.Empty : imageurl.Value,
                        EbayTitle = title == null ? String.Empty : title.Value,
                        EbayUrl = url == null ? String.Empty : url.Value,
                        NumberOfWatch = nofwatch == null ? String.Empty : nofwatch.Value,
                    });
                }