使用C#中的LINQ根据值迭代xmldocument [英] Iterate through xmldocument depending on value using LINQ in C#

查看:67
本文介绍了使用C#中的LINQ根据值迭代xmldocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,

我有xml看起来像这样:

 <   Root  >  
< 类别 TopCategory = txt 1 MediumCategory = txt 2 SubCategory = txt 3 / >
< 类别 TopCategory = txt 4 MediumCategory = txt 4 SubCategory = txt 5 / >
< 类别 TopCategory = txt 7 MediumCategory = txt 8 SubCategory = txt 9 / >
< 类别 TopCategory = txt 10 MediumCategory = txt 11 SubCategory = txt 12 / >
< 类别 TopCategory = txt 13 MediumCategory = txt 14 SubCategory = txt 15 / >
< 类别 TopCategory = txt 16 MediumCategory = txt 17 SubCategory = txt 18 / >
< / Root >





现在,xml传递给XmlDocument对象,我想要的是提取TopCategory& MediumCategory仅使用LINQ的SubCategory==txt 12(例如)的值。

任何想法?请帮忙......



我尝试过的事情:



.................................................. .....

解决方案

本网站将向您展示如何:带有示例的LINQ to XML教程| DotNetCurry [ ^ ] - 参见第5点。如何使用LINQ to XML访问具有特定属性的特定元素 - 易于适应您的要求


作为解决方案1的替代方法< a href =https://www.codeproject.com/script/Membership/View.aspx?mid=1315955> Graeme_Grant [ ^ ],我想说...



MSDN文档非常好。我建议首先:

LINQ to XML Overview(C#) [ ^ ]

LINQ to XML(C#) [ ^ ]

基本查询(LINQ to XML)(C#) [ ^ ],尤其是:如何:查找具有特定属性的元素(C#) [ ^ ]



我会以这种方式解决您的问题:



  string  xcontent =  @ < ;根和GT; 
< Categories TopCategory ='txt 1'MediumCategory ='txt 2'SubCategory ='txt 3'/>
< Categories TopCategory ='txt 4'MediumCategory ='txt 4'SubCategory ='txt 5'/>
< Categories TopCategory ='txt 7'MediumCategory ='txt 8'SubCategory ='txt 9'/>
< Categories TopCategory ='txt 10'MediumCategory ='txt 11'SubCategory ='txt 12'/>
< Categories TopCategory ='txt 13'MediumCategory ='txt 14'SubCategory ='txt 15'/>
< Categories TopCategory ='txt 16'MediumCategory ='txt 17'SubCategory ='txt 18'/>
< / Root>
;

XmlReader rdr = XmlReader.Create( new StringReader(xcontent)) ;
XDocument xdoc = XDocument.Load(rdr);

var result = xdoc.Descendants()
。 Where(x = > string )x.Attribute( SubCategory)== txt 12);





以上代码应返回( IEnumerable< XElement> ):

< Categories TopCategory =txt 10MediumCategory =txt 11SubCategory =txt 12/> 


Hey guys,
I have xml looks like that:

<Root>
  <Categories TopCategory="txt 1" MediumCategory="txt 2" SubCategory="txt 3" />
  <Categories TopCategory="txt 4" MediumCategory="txt 4" SubCategory="txt 5" />
  <Categories TopCategory="txt 7" MediumCategory="txt 8" SubCategory="txt 9" />
  <Categories TopCategory="txt 10" MediumCategory="txt 11" SubCategory="txt 12" />
  <Categories TopCategory="txt 13" MediumCategory="txt 14" SubCategory="txt 15" />
  <Categories TopCategory="txt 16" MediumCategory="txt 17" SubCategory="txt 18" />
</Root>



Now, the xml is passing to an XmlDocument object and what i want is extracting "TopCategory" & "MediumCategory" only where the value of "SubCategory" == "txt 12" (for example) using LINQ.
Any Ideas? please help....

What I have tried:

.......................................................

解决方案

This website will show you how: LINQ To XML Tutorials with Examples | DotNetCurry[^] - see point 5. "How Do I access Specific Element having a Specific Attribute using LINQ to XML" - easy to adapt to your requirements


As an alternative to solution 1 by Graeme_Grant[^], i'd like to say...

MSDN documentation is very good. I'd suggest to start with:
LINQ to XML Overview (C#)[^]
LINQ to XML (C#)[^]
Basic Queries (LINQ to XML) (C#)[^], especially: How to: Find an Element with a Specific Attribute (C#)[^]

I'd resolve your issue this way:

string xcontent = @"<Root>
  <Categories TopCategory='txt 1' MediumCategory='txt 2' SubCategory='txt 3' />
  <Categories TopCategory='txt 4' MediumCategory='txt 4' SubCategory='txt 5' />
  <Categories TopCategory='txt 7' MediumCategory='txt 8' SubCategory='txt 9' />
  <Categories TopCategory='txt 10' MediumCategory='txt 11' SubCategory='txt 12' />
  <Categories TopCategory='txt 13' MediumCategory='txt 14' SubCategory='txt 15' />
  <Categories TopCategory='txt 16' MediumCategory='txt 17' SubCategory='txt 18' />
</Root>";

XmlReader rdr = XmlReader.Create(new StringReader(xcontent));
XDocument xdoc = XDocument.Load(rdr);

var result = xdoc.Descendants()
		.Where(x=>(string)x.Attribute("SubCategory")=="txt 12");



Above code should return (IEnumerable<XElement>):

<Categories TopCategory="txt 10" MediumCategory="txt 11" SubCategory="txt 12" />


这篇关于使用C#中的LINQ根据值迭代xmldocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆