如何筛选XmlNodeList

3

我有一组数据的XML节点列表,我想根据特定属性的内部文本进行过滤,但是我尝试了以下代码却没有任何作用。这里是我的代码,同时也发布XML数据。

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


分享你的 XML 文件。 - Chandan Kumar
see the updated question - Mehaboob
2个回答

4

我已经完成了小的更改,删除了属性。

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

非常完美的工作!!!


0

使用 Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Category.categories = doc.Descendants("Category").Select(x => new Category() {
                BestOfferEnabled = (Boolean)x.Element("BestOfferEnabled"),
                AutoPayEnabled = (Boolean)x.Element("BestOfferEnabled"),
                CategoryID = (int)x.Element("CategoryID"),
                CategoryLevel = (int)x.Element("CategoryLevel"),
                CategoryName = (string)x.Element("CategoryName"),
                CategoryParentID = (int)x.Element("CategoryParentID"),
            }).ToList();

            int id = 37903;

            Category categoryId = Category.categories.Where(x => x.CategoryParentID == id).FirstOrDefault();
        }
    }
    public class Category
    {
        public static List<Category> categories { get; set; }

        public Boolean  BestOfferEnabled { get; set; }        
        public Boolean  AutoPayEnabled { get; set; }

        public int CategoryID { get; set; }
        public int CategoryLevel { get; set; }
        public string CategoryName { get; set; }
        public int CategoryParentID { get; set; }

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接