【问题标题】:How to extract all nodes of same level如何提取同一级别的所有节点
【发布时间】:2014-11-29 13:28:51
【问题描述】:

我正在尝试获取所有文件夹 caption 属性并希望存储在列表中

下面是我的 XML 文件

<?xml version="1.0" encoding="utf-16" ?>
<Folders Name="MyFolderName">
  <Folder Caption="Bank">
    <Card Caption="BankName1">
      <Property Type="String" Caption="Bank">Bank1</Property>
    </Card>
    <Card Caption="BankName2">
      <Property Type="String" Caption="Bank">Bank2</Property>
    </Card>
  </Folder>
  <Folder Caption="Bills">
    <Card Caption="BillName1">
      <Property Type="Numeric" Caption="BillName">BillName1Data</Property>
    </Card>
    <Card Caption="BillName2">
      <Property Type="Numeric" Caption="BillName1">BillName2Data</Property>
    </Card>
  </Folder>
</Folders>

下面是我的查询

public static List<Folder> ExtractFolders()
    {
        XDocument doc = XDocument.Load(@"I:\WindowsPhone\xmlTesting\xmlTesting\Data\VaultData.xml");
        List<Folder> folders = (from c in doc.Descendants("Folders")
                                select new Folder()
                                {
                                    Caption = c.Element("Folder").Attribute("Caption").Value
                                }).ToList<Folder>();
        return folders;
    }

我只得到第一个文件夹

我怎样才能得到文件夹列表

【问题讨论】:

    标签: c# xml linq linq-to-xml xelement


    【解决方案1】:

    改变

        List<Folder> folders = (from c in doc.Descendants("Folders")
                                select new Folder()
                                {
                                    Caption = c.Element("Folder").Attribute("Caption").Value
                                }).ToList<Folder>();
    

        List<Folder> folders = (from c in doc.Descendants("Folder")
                                select new Folder()
                                {
                                    Caption = c.Attribute("Caption").Value
                                }).ToList<Folder>();
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    相关资源
    最近更新 更多