0

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

Nadeem Khan
  • 1
  • 1
  • 3

1 Answers1

1

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>();
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks a lot sir, it really help me. I have another problem, I want to find all the card in particular folder, for example I want to find all the cards in Bank folder – Nadeem Khan Dec 01 '14 at 16:24
  • Ask a new question for a new problem but start with reading the LINQ introduction on MSDN http://msdn.microsoft.com/en-us/library/bb397933%28v=vs.110%29.aspx, the section http://msdn.microsoft.com/en-us/library/bb397927%28v=vs.110%29.aspx should help. – Martin Honnen Dec 01 '14 at 16:31