2017-06-03 52 views
0

我有一組數據xml節點列表,我想過濾一個特定的屬性內部文本,我試過這個,但沒有任何工作。我的代碼在這裏,也張貼的XML數據我如何過濾XmlNodeList

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 =我的價值。

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

+0

分享您的XML文件 –

+0

看到更新後的問題 – Mehaboob

回答

0

我已經做了拆除了小的改變屬性

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

    } 
}