0

I can't seem to parse my xml string to a custom object. Here is my code:

    private Account parseXmlToAccount (String xml)
    {
        XDocument doc = XDocument.Parse(xml);
        Console.Out.WriteLine("a" + xml);
        List<Account> result = 
            (
                from x in doc.Root.Elements("user")
                select new Account()
                {
                    Session = (string) x.Element("session").Value,
                    User = (string) x.Element("user").Value
                }).ToList();
        Console.Out.WriteLine("b");
        return result.First();

And Account.cs:

class Account
{
    public string Session { get; set; }
    public string User { get; set; }

    public override string ToString()
    {
        return Session + " " + User;
    }
}

and my xml string:

<user>
  <session>7981239761293767891</session>
  <user>873948797862364</user>
</user>

and my error message:

UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at testMonoAndroidApp.LoginManager.<parseXmlToAccount>m__4 (System.Xml.Linq.XElement) <0x00044>
at System.Linq.Enumerable/<CreateSelectIterator>c__Iterator27`2<System.Xml.Linq.XElement, testMonoAndroidApp.Account>.MoveNext () <0x0015b>
at System.Collections.Generic.List`1<testMonoAndroidApp.Account>.AddEnumerable (System.Collections.Generic.IEnumerable`1<testMonoAndroidApp.Account>) <0x000ab>
at System.Collections.Generic.List`1<testMonoAndroidApp.Account>..ctor (System.Collections.Generic.IEnumerable`1<testMonoAndroidApp.Account>) <0x00093>
at System.Linq.Enumerable.ToList<testMonoAndroidApp.Account> (System.Collections.Generic.IEnumerable`1<testMonoAndroidApp.Account>) <0x0003b>
at testMonoAndroidApp.LoginManager.parseXmlToAccount (string) <0x0012f>
...
2
  • Have you stepped through your code in a debugger? If not then do that. If you have which line is throwing the exception? Commented Feb 15, 2013 at 8:45
  • I have stepped trough with the debugger (MonoDevelop). The exception is cast on the 5th line in my first code part (List<Account> result = ) Commented Feb 15, 2013 at 8:47

2 Answers 2

3

Your root is the user element, so change:

from x in doc.Root.Elements("user")

to

from x in doc.Elements("user")
Sign up to request clarification or add additional context in comments.

2 Comments

A user has two elements: session and user.
There is only one user at the top level, so I assume it should produce a list of one user. So no null values there?
3
List<Account> result = (from o in doc.Elements()
             select new Account{ 
               User = o.Element("user").Value, 
               Session = o.Element("session").Value 
             }).ToList();

Comments

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.