1

I have an xml file like this:

<users>
    <user name="user" password="123" email="[email protected]"/>
</users>

I need to write a code to copy the attribute values to a object type variable and I can't find anything which suits my needs. some part of the code which I have successfully written is:

public static UserInfo GetUser()
{
    XDocument users = XDocument.Load(FilePath.UserConfigurationPath);

    UserInfo usersvar = new UserInfo();
}

Here I have to return the object and compare it with a textbox value.

Can anybody please tell me how I can copy the values to the object?

4
  • 2
    "return d object" -- seriously? :) Commented Aug 29, 2011 at 11:54
  • 2
    @JohnD Don't mock other people's English skills. Commented Aug 29, 2011 at 11:55
  • 1
    Using google and the documentation this is a really simple task. What have you tried so far? Commented Aug 29, 2011 at 11:55
  • @Oskar Not trying to mock, it sounded more like he was using cool-speak with "dis" instead of "this" and "d" instead of "the". Maybe I'm mistaken - just sounded funny. Commented Aug 29, 2011 at 12:01

1 Answer 1

3

To parse all the users:

IEnumerable<UserInfo> GetUsers()
{
    XDocument users = XDocument.Load(path);

    return from u in users.Descendants("user")
           select new UserInfo
           {
               Name = (string)u.Attribute("name"),
               Password = (string)u.Attribute("password"),
               Email = (string)u.Attribute("email")
           };
}

IEnumerable<UserInfo> users = GetUsers();
UserInfo userUser = users.FirstOrDefault(u => u.Name == "user");

If the document contains exactly one user or you want to parse exactly the first:

XElement userElement = users.Descendants("user").FirstOrDefault();
if (userElement != null)
{
    UserInfo user = new UserInfo
    {
        Name = (string)userElement .Attribute("name"),
        Password = (string)userElement .Attribute("password"),
        Email = (string)userElement .Attribute("email")
    };
}
Sign up to request clarification or add additional context in comments.

5 Comments

Are those commas supposed to be there after your type-casts?
Yea, i figured the editor put them in and you didn't notice them. Anyways +1
Thanks abatishchev, with a little tweak the code is working perfectly.
Instead of doing the filter in the FirstOrDefault after you have projected the entity, why not do it in the where clause before the projection. Thay way you are not hydrating objects unnecessarily. I.e. if you have 20 user nodes and your filter criteria matches item 19, you will create 19 instances of the UserInfo class before finding the one you want. Putting the filter before the projection will allow you to only hydrate one UserInfo object.
@Tyson: Thanks! :) And see you around here, on SO.

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.