C# 使用linq查询xmlnode
我有以下文件:C# 使用linq查询xmlnode,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我有以下文件: <root> <Product desc="Household"> <Product1 desc="Cheap"> <Producta desc="Cheap Item 1" category="Cooking" /> <Productb desc="Cheap Item 2" category="Gardening" /> </Product1>
<root>
<Product desc="Household">
<Product1 desc="Cheap">
<Producta desc="Cheap Item 1" category="Cooking" />
<Productb desc="Cheap Item 2" category="Gardening" />
</Product1>
<Product2 desc="Costly">
<Producta desc="Costly Item 1" category="Decoration"/>
<Productb desc="Costly Item 2" category="Furnishing" />
<Productc desc="Costly Item 3" category="Pool" />
</Product2>
</Product>
</root>
您的XML结构很不幸,因为它在三个层次结构中使用了Product元素。你有其他类似于“家庭”的元素吗 假设我们只需要家庭用品,您可以使用: 清点每种便宜/昂贵商品的数量
xe.Element("Product") // Select the Product desc="household" element
.Elements() // Select the elements below it
.Select(element => new { Name=(string) element.Attribute("desc"),
Count=element.Elements().Count() });
列出所有类别
xe.Descendants() // Select all descendant elements
.Attributes() // All attributes from all elements
// Limit it to "category" elements
.Where(attr => attr.Name == "category")
// Select the value
.Select(attr => attr.Value)
// Remove duplicates
.Distinct();
xe.Descendants() // Select all elements
// Only consider those with a "Costly" description
.Where(element => (string) element.Attribute("desc") == "Costly")
// Select the subelements of that element, and flatten the result
.SelectMany(element => element.Elements());
要对此进行排序,只需在末尾使用.OrderBy(x=>x)
选择“昂贵”产品
xe.Descendants() // Select all descendant elements
.Attributes() // All attributes from all elements
// Limit it to "category" elements
.Where(attr => attr.Name == "category")
// Select the value
.Select(attr => attr.Value)
// Remove duplicates
.Distinct();
xe.Descendants() // Select all elements
// Only consider those with a "Costly" description
.Where(element => (string) element.Attribute("desc") == "Costly")
// Select the subelements of that element, and flatten the result
.SelectMany(element => element.Elements());
我个人觉得使用
XmlDocument
更容易:
XmlDocument root = new XmlDocument();
root.LoadXml(xml); // or .Load(path);
var categories = root.SelectNodes(
"/root/Product/Product/Product/@category")
.Cast<XmlNode>().Select(cat => cat.InnerText).Distinct();
var sortedCategories = categories.OrderBy(cat => cat);
foreach (var category in sortedCategories)
{
Console.WriteLine(category);
}
var totalItems = root.SelectNodes(
"/root/Products/Product/Product").Count;
Console.WriteLine(totalItems);
foreach (XmlElement prod in root.SelectNodes(
"/root/Product/Product[@desc='Costly']/Product"))
{
Console.WriteLine(prod.GetAttribute("desc"));
}
XmlDocument root=new XmlDocument();
root.LoadXml(xml);//加载(路径);
var categories=root.SelectNodes(
“/root/Product/Product/Product/@category”)
.Cast().Select(cat=>cat.InnerText).Distinct();
var sortedCategories=categories.OrderBy(cat=>cat);
foreach(分类类别中的var类别)
{
控制台写入线(类别);
}
var totalItems=root.SelectNodes(
“/root/Products/Product/Product”)。计数;
控制台写入线(总计项);
foreach(root.SelectNodes中的XmlElement prod(
“/root/Product/Product[@desc='cost']/Product”))
{
Console.WriteLine(prod.GetAttribute(“desc”);
}
我真的需要自学LINQ。。。这样听起来很简单:)我怀疑Jon是否有意让您重命名数据中的单个元素;相反,在每个级别都有不同的名称…好的..对不起..您知道,在前面的xml中,所有内容都是产品,当我使用您的第一个查询时,它给我的对象未设置为引用errorHmm。。。它应该工作得很好,因为我使用您提供的XML(加上两个“/”来终止元素)对它进行了测试。感谢您的及时回复。请详细说明(加上两个“/”以终止元素)?