C# Don';t工作的Linq.xml模板
我需要从满足条件的XML文件中获取属性列表。例如:C# Don';t工作的Linq.xml模板,c#,xml,linq,C#,Xml,Linq,我需要从满足条件的XML文件中获取属性列表。例如: <rows> <row code="16" type="M" grv="К"> <cell column="К" dic="s_okved" format="C(8)" inputType="1" vldType="4" vld="pril_okved_11" /> </row> <row code="17" typ
<rows>
<row code="16" type="M" grv="К">
<cell column="К" dic="s_okved" format="C(8)" inputType="1" vldType="4"
vld="pril_okved_11" />
</row>
<row code="17" type="M" grv="К">
<cell column="К" dic="s_okved" format="C(8)" inputType="1" vldType="2"
vld="pril" />
</row>
</rows>
我觉得这是对的,但它不起作用。有什么想法吗?你就在附近。尝试:
XDocument.Parse(xml)
.Descendants()
.First(e => e.Attribute("vldType") != null && e.Attribute("vldType").Value == "4")
.Attribute("vld")
.Value;
或者如果您使用的是C#6:
在尝试获取测试属性的值之前,需要验证测试属性vldType
是否存在。如果它不存在,请继续
此外,您可以在此处将
Where
替换为First
,因为您需要第一个结果。此外,您还可以删除.Root
和.Elements
,然后直接转到子体
,非常感谢!我错过了生存的支票。Workingtemplate.Root.Elements().subjects()。其中(e=>e.Attribute(“vldType”)!=null和&e.Attribute(“vldType”)。Value==“4”).Attributes(“vld”);如果这个答案解决了你的问题,我将感谢你点击旁边的绿色复选框
XDocument.Parse(xml)
.Descendants()
.First(e => e.Attribute("vldType") != null && e.Attribute("vldType").Value == "4")
.Attribute("vld")
.Value;
XDocument.Parse(xml)
.Descendants()
.First(e => e.Attribute("vldType")?.Value == "4")
.Attribute("vld")
.Value;