如何获得XElement的价值

sca*_*t17 0 c#

我正在解析xml文档,我需要获取Property元素的值.截至目前,我有一行代码返回:

<Property name="ID" value="thevalueineed"/>
Run Code Online (Sandbox Code Playgroud)

这是我使用的代码行.

var ID = from el in linkedinfo.DescendantsAndSelf("Property") 
         where (string)el.Attribute("name") == "ID" 
         select el.Attributes("value").ToString();
Run Code Online (Sandbox Code Playgroud)

我会错过的下一步是什么,我可以thevalueineed从我所拥有的那个元素中得到var ID什么?

EZI*_*EZI 5

只需改变你select

select (string)el.Attribute("value")
Run Code Online (Sandbox Code Playgroud)

一个工作代码.

var xDoc = XDocument.Parse(@"<root><Property name=""ID"" value=""thevalueineed""/></root>");

var ID = from el in xDoc.Root.DescendantsAndSelf("Property")
            where (string)el.Attribute("name") == "ID"
            select (string)el.Attribute("value");

var val = ID.First();
Run Code Online (Sandbox Code Playgroud)