微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用LINQ to XML检索更深的兄弟姐妹?

如何解决如何使用LINQ to XML检索更深的兄弟姐妹?

| 我有一个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>
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。