0
votes
public FileEntry ReadFileConfiguration(string id)
{
    string configurationPath = "conf.xml";
    XDocument data = XDocument.Load(configurationPath);
    return (from c in data.Descendants("file")
        where (c.Attribute("Id") != null && c.Attribute("Id").Value.Equals(id)) 
        select new FileEntry()
        {
            Name = c.Element("Name").Value,
            Path = c.Element("Path").Value,
            SheduledTime = Convert.ToDateTime(c.Element("SheduledTime").Value),
            Size = (long)Convert.ToDouble( c.Element("Size").Value),
            IsFolder = Convert.ToBoolean( c.Element("IsFolder").Value),
            LastAccess = Convert.ToDateTime(c.Element("LastAccess").Value),
            DoEncrypt = Convert.ToBoolean( c.Element("DoEncrypt").Value)
        }).First();
}

Main program is:

main()
{
    string id  = "C:\\Users\\Radhesh\\Documents\\Visual Studio 2008\\Projects\\Rme\\Rme\\test.txt";
    ReadFileConfiguration(id);
}

My XML page is:

<?xml version="1.0" encoding="utf-8" ?> 
<Files>
    <file Id="C:\Users\Radhesh\Documents\Visual Studio 2008\Projects\Rme\Rme\test.txt">
        <Name>test.txt</Name> 
        <Path>C:\Users\Radhesh\Documents\Visual Studio 2008\Projects\Rme\Rme\test.txt</Path> 
        <IsFolder>False</IsFolder> 
        <DoEncrypt>True</DoEncrypt> 
        <Size>0</Size> 
        <LastAcess>7/9/2011 11:35:53 PM</LastAcess> 
        <SheduledTime>7/10/2011 1:59:20 PM</SheduledTime> 
    </file>
</Files>

My class:

class FileEntry
{
        public string Name { get;set;}
        public string Path { get; set; }
        public bool IsFolder { get; set; }
        public long Size { get;set; }
        public DateTime LastAccess { get;set; }
        public DateTime SheduledTime { get; set; }
        public bool DoEncrypt { get;  set; }

}

Please, can anyone help?

2
On which line is the exception thrown? - hammar
Can you at least take the time to format your question? - Jesus Ramos
List the line# and stacktrace from the exception. - Henk Holterman
You need to pass the full path to the XML file, for example: string configurationPath = @"C:\Users\Radhesh\Documents\Visual Studio 2008\Projects\Rme\Rme\conf.xml"; - Shadow Wizard Is Vaccinated V3
@Shadow: depends on where the conf file is and I think .Load() will throw something other than NullRef when the path is wrong. - Henk Holterman

2 Answers

4
votes

Edit, just spotted it

 <LastAcess>7/9/2011 11:35:53 PM</LastAcess> 

 LastAccess = Convert.ToDateTime(c.Element("LastAccess").Value),

Notice: "Acess" and Access


Start by breaking it down.

string configurationPath = "conf.xml";
XDocument data = XDocument.Load(configurationPath);
var files = from c in data.Descendants("file") select c;

Debug.WriteLine("Count = {0}", files.Count());

...
return files.First();

This should output 1 (or higher).

If it succeeds, add the where clause before the select.

Then change to the full select part and add the Name = c.Element("Name").Value, lines 1 by 1.

1
votes

I followed the steps that @Henk Holterman suggested, and here's why you're seeing the Null Reference Exception. In your select:

LastAccess = Convert.ToDateTime(c.Element("LastAccess").Value)

If you look at your XML file, you have:

<LastAcess>7/9/2011 11:35:53 PM</LastAcess>

You have, in a nutshell, a typo. The select portion of the query couldn't find an element named "LastAccess", therefore throwing the Null Reference Exception.