0

Basically I have a single element inside of an xml file where I store settings for my application. This element mirrors a class that I have built. What I'm trying to do using LINQ, is select that single element, and then store the values stored inside of that element into an instance of my class in a single statement.

Right now I'm selecting the element seperately and then storing the values from that element into the different properties. Of course this turns into about six seperate statements. Is it possible to do this in a single statement?

2 Answers 2

2

It will be better if you can show your XML but you can get general idea from code below

XDocument doc = //load xml document here

var instance = from item in doc.Descendants("ElementName")
                   select new YourClass()
                              {
                                  //fill the properties using item 
                              };
Sign up to request clarification or add additional context in comments.

Comments

2

You can use LINQ to XML, e.g.

var document = XDocument.Load("myxml.xml");
document.Element("rootElement").Element("myElement").Select(e => 
  new MySettingsClass
  {
    MyProperty = e.Attribute("myattribute").Value,
    MyOtherProperty = e.Attribute("myotherattribute").Value 
  });

See http://msdn.microsoft.com/en-us/library/bb387098.aspx for more details.

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.