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

8
  • 1
    On which line is the exception thrown? Commented Jul 10, 2011 at 8:55
  • 1
    Can you at least take the time to format your question? Commented Jul 10, 2011 at 8:58
  • List the line# and stacktrace from the exception. Commented Jul 10, 2011 at 9:01
  • 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"; Commented Jul 10, 2011 at 9:03
  • @Shadow: depends on where the conf file is and I think .Load() will throw something other than NullRef when the path is wrong. Commented Jul 10, 2011 at 9:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

@harmmer Exception is thrown at return statement after select new FileEntry().
You commented under the wrong post. But this means the exception is from code that is not listed?
Thanks for replying. But I think we cannot write a query with out select statement.So breaking it itself giving error
:I didn't get you. "Object reference not set to an instance of an object.", this is the additional Information I am getting for the exception.
Btw, I think you should accept @Henk's answer - it was his method of breaking it up that led to the discovery of the typo.
|
1

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.

1 Comment

Typos happen :) Remember the technique @Henk gave you - sometimes the best way to track down seemingly obscure bugs is to break it down until you can find the real root cause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.