How to extract all nodes of same level

491 views Asked by At

I am trying to get all folder caption attribute and want to store in list

Below is my XML File

<?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>

below is my query

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;
    }

I am getting only first folder

How can I can I get list of folders

1

There are 1 answers

2
Martin Honnen On

Change

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

to

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