我如何过滤 XmlNodeList [英] How can i filter XmlNodeList

查看:27
本文介绍了我如何过滤 XmlNodeList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据 xml 节点列表,我想使用特定属性的内部文本进行过滤,我尝试了这个,但没有任何效果.我的代码在这里并且还发布了 xml 数据

I have a set of data xml node list, i want to filter with a particular attributes inner text, I tried with this but nothing worked. My code here and also posting the xml data

string baseName = categoryName.Split(':').Last();
            int categoryId = 0;
            string xmlFile = File.ReadAllText(Application.StartupPath + @"\EbayCategories\EbayCategories.xml");
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xmlFile);
            XmlNodeList nodeList = xmldoc.SelectNodes("/CategoryArray/Category[CategoryName='" + baseName + "']");
            if (nodeList.Count > 0)
            {
                var memberNames = nodeList.Cast<XmlNode>().Where(node => node.Attributes["CategoryID"].InnerText == "58193").ToList();
                categoryId = int.Parse(nodeList[0]["CategoryID"].InnerText);
            }

这是我的 xml 数据.我想过滤 CategoryParentID = My Value.

Here is my xml Data. I want to filter CategoryParentID = My Value.

<CategoryArray>
    <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <AutoPayEnabled>true</AutoPayEnabled>
        <CategoryID>20081</CategoryID>
        <CategoryLevel>1</CategoryLevel>
        <CategoryName>Antiques</CategoryName>
        <CategoryParentID>20081</CategoryParentID>
    </Category>
    <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <AutoPayEnabled>true</AutoPayEnabled>
        <CategoryID>37903</CategoryID>
        <CategoryLevel>2</CategoryLevel>
        <CategoryName>Antiquities</CategoryName>
        <CategoryParentID>20081</CategoryParentID>
    </Category>
    <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <AutoPayEnabled>true</AutoPayEnabled>
        <CategoryID>37908</CategoryID>
        <CategoryLevel>3</CategoryLevel>
        <CategoryName>The Americas</CategoryName>
        <CategoryParentID>37903</CategoryParentID>
        <LeafCategory>true</LeafCategory>
    </Category>
    <Category>
        <BestOfferEnabled>true</BestOfferEnabled>
        <AutoPayEnabled>true</AutoPayEnabled>
        <CategoryID>162922</CategoryID>
        <CategoryLevel>3</CategoryLevel>
        <CategoryName>Byzantine</CategoryName>
        <CategoryParentID>37903</CategoryParentID>
        <LeafCategory>true</LeafCategory>
    </Category>

推荐答案

我已经做了一些小的改动 Removed Attributes

I have done with small changes Removed Attributes

var node = nodeList.Cast<XmlNode>().Where(n => n["CategoryParentID"].InnerText == "58193").Select(x => x["CategoryID"].InnerText).SingleOrDefault();

完美运行!!!

这篇关于我如何过滤 XmlNodeList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆