2017-07-28 31 views
-4
<Content> 
    <Field Title="123"> 
     <Description>...</Description> 
     <Comment>...</Comment> 
    </Field> 
    <Field Title="654"> 
     <Description>....</Description> 
     <Comment>...</Comment> 
    </Field> 
    <Field Title="789"> 
     <Description>...</Description> 
     <Comment>...</Comment> 
    </Field> 
    <Field Title="210"> 
     <Description>...</Description> 
     <Comment>...</Comment> 
    </Field>  
</Content> 

您好,我想提取所有现场节点属性名称值转换成使用LINQ的列表。所以列表必须包含“123”,“654”,“789”,“210”任何人都可以给我一个解决方案吗?选择-Linq的所有属性值,为xml

我尝试这样做:

var fldLst = from myEl in myXmlDoc.Root.Descendants() 
     where myEl.Name.LocalName 
     select (string)myEl.Attribute("title"); 

其中myXmlDoc是XML文档。

+4

欢迎。你的问题本质上是一系列的要求_。 [问] – MickyD

+1

请显示你的尝试,解释什么是不工作,你已经试图解决它 –

+0

我试过这个: var fldLst = myEl in myXmlDoc.Root.Descendants() where myEl.Name .LocalName select(string)myEl.Attribute(“title”); 其中myXmlDoc是XML文档。 – TTORNADE

回答

0

您需要使用DescendantsAttribute方法:

var result= doc.Descendants("Field").Select(e=>(string)e.Attribute("Title")).ToList();