问题描述
|
我有一个XML结构,如下所示。我需要通过匹配命令属性来提取\“ Value \”和\“ String \”吗?如何为此编写LINQ?
<Root>
<Command val=\"1001\" type=\"sync\">
<Status>
<DataList>
<Info>
<Value>1</Value>
<String>Sample String 1 is set</String>
</Info>
<Info>
<Value>2</Value>
<String>Sample String 2 is set</String>
</Info>
<Info>
<Value>3</Value>
<String>Sample String 3 is set</String>
</Info>
</DataList>
</Status>
<Command>
</Root>
我尝试了以下操作,但运行时发生异常。
lst = (
from command in xmlDoc.Descendants(\"Command\")
.Descendants(\"Status\")
.Descendants(\"DataList\")
select new EnumList
{
val = command.Element(\"Value\").Value,stringVal = command.Element(\"String\").Value,})
.ToList();
解决方法
尝试
lst = (
from command in xmlDoc.Descendants(\"Info\")
select new EnumList
{
val = command.Element(\"Value\").Value,stringVal = command.Element(\"String\").Value,})
.ToList();
并且您在xml示例中有错误(没有关闭标记命令),请将其更改为
</Command>
</Root>