【问题标题】:How can i filter XmlNodeList我如何过滤 XmlNodeList
【发布时间】:2017-06-03 06:41:50
【问题描述】:

我有一组数据 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文件
  • 查看更新后的问题

标签: c# xml winforms


【解决方案1】:

我已经完成了删除属性的小改动

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

完美的工作!!!

【讨论】:

    【解决方案2】:

    使用 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; }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      相关资源
      最近更新 更多