How to list xml Element via XDocument in c#?

1.6k views Asked by At

I have read a lot of topics here regarding xml and tried some, but still I can't get work of this problem.
I have to list the "Items" from my xml file and load it to a ListView. My project is made in Pocket-PC. Here is the sample xml content.

<? xml version="1.0" encoding="utf-8" ?>
<Library>
    <Item>
        <Name>Picture</Name>
        <FullPath>\My Device\My Documents\Picture</FullPath>
        <SystemFullpath>\Program Files\Explorer\Library</SystemFullpath>
        <Created>0001-01-01T00:00:00</Created>
    </Item>
    <Item>
        <Name>Video</Name>
        <FullPath>\My Device\My Documents\Video</FullPath>
        <SystemFullpath>\Program Files\Explorer\Library</SystemFullpath>
        <Created>0001-01-01T00:00:00</Created>
    </Item>
    <Item>
        <Name>File</Name>
        <FullPath>\My Device\My Documents\File</FullPath>
        <SystemFullpath>\Program Files\Explorer\Library</SystemFullpath>
        <Created>0001-01-01T00:00:00</Created>
    </Item>
</Library>  

The way I add an Item:

public bool AddLibrary(Library lib)
{
    try
    {
        XDocument xDoc = XDocument.Load(fileName);
        XElement xe = new XElement("Item",
            new XElement("Name", lib.Name),
            new XElement("Fullpath", lib.Fullpath),
            new XElement("SystemFullpath", lib.SystemFullpath),
            new XElement("Created", lib.Created));

        xDoc.Element("Library").Add(xe);
        xDoc.Save(fileName);
        return true;
    }
    catch { return false; }
}  

The Library Entity:

public class Library
{
    public Library() { }

    // Unique
    public string Name { get; set; }

    public string Fullpath { get; set; }

    public string SystemFullpath { get; set; }

    public DateTime Created { get; set; }

    public List<Items> Items { get; set; }
}  

And the code for retrieving items that returns an error:

public List<Library> RetrieveAllLibrary()
{
    List<Library> libList = new List<Library>();
    if (File.Exists(fileName))
    {
        XDocument xDoc = XDocument.Load(fileName);

        var items = from item in xDoc.Descendants("Item")
                    select new
                    {
                        Name = item.Element("Name").Value,
                        FullPath = item.Element("FullPath").Value,
                        Created = item.Element("Created").Value
                    };

        if (items != null)
        {
            foreach (var item in items)
            {
                Library lib = new Library();
                lib.Name = item.Name;
                lib.Fullpath = item.FullPath;
                lib.Created = DateTime.Parse(item.Created);
                libList.Add(lib);
            }
        }
    }
    return libList;
}  

The error says:

enter image description here

I hope I'd explain it well. Thanks for help!!

1

There are 1 answers

1
Markus Jarderot On BEST ANSWER

Your problem is this line:

new XElement("Fullpath", lib.Fullpath),

The name is typed with a lower case "p", and later you used "FullPath" with a capital "P".

You should also replace all "Fullpath" with "FullPath" in the XML-file, if you want to keep the data.