我有一组数据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>
答案 0 :(得分:1)
我完成了一些小修改删除了属性
var node = nodeList.Cast<XmlNode>().Where(n => n["CategoryParentID"].InnerText == "58193").Select(x => x["CategoryID"].InnerText).SingleOrDefault();
完美的工作!!!
答案 1 :(得分: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; }
}
}