?
Solved

Retrieve complex data in Linq to XML

Posted on 2011-09-13
5
Medium Priority
?
345 Views
Last Modified: 2012-05-12
I have an XML data source having the following layout:
<Items>
<Item Name="Bob" Color="Green" />
<Item Name="Carol" Color="Red">
  <Pet Type="Cat" Age="3" />
  <Pet Type="Dog" Age="7" />
</Item>
<Item Name="Ted" Color="Blue" />
</Items>

Open in new window


I want to use Linq to XML to get this data into an Items structure defined as follows:
public class Item
{
  public string Name {get; set;}
  public string Color {get; set;}
  public List<Pet> Pets {get; set;}
}

Open in new window


public class Pet
{
  public string Type {get; set;}
  public int Age {get; set;}
}

Open in new window


I'm trying to do something like this:
List<Item> myItems = from e in XElement.Load("MyData.xml").Elements("Item")
  select new Item
  {
    Name = (string)e.Attribute("Name"),
    Color = (string)e.Attribute("Color"),
    Pets = (List<Pet>)e.Descendants()
  }

Open in new window


The above Linq statement is wrong, but should give a good idea of what I'm trying to achieve. I can get Name and Color, but can't figure out how to get the list of Pets.

Thanks for helping!


0
Comment
Question by:wlevy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36528802
Hi!
Try
List<Item> myItems = from e in XElement.Load("MyData.xml").Elements("Item")
  select new Item
  {
    Name = (string)e.Attribute("Name"),
    Color = (string)e.Attribute("Color"),
    Pets = e.Descendants().ToList<Pet>()
  }
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36528899
Validate your xml is correctly formatted.

XDocument xmlDoc = XDocument.LoadXML("your xml in string format"); //Load method to load a xml file
var q = from c in xmlDoc.Descendants("Item")
            select new Item()
{
 Name = c.Attribute("Name").Value;
Color = c.Attribute("Color").Value;
Pets = from v in c.XMLElement("Pets")
         select new Pet()
         {
          Type = v.Attribute("Type").Value;
          Age = v.Attribute("Age").Value;

         }

}

http://blogs.msdn.com/b/wriju/archive/2008/02/18/linq-to-xml-creating-complex-xml-through-linq.aspx
http://www.joe-stevens.com/2010/01/08/linq-to-xml-tutorial/
http://www.techrepublic.com/blog/programming-and-development/access-xml-data-using-linq-to-xml/594
http://www.google.co.in/#sclient=psy&hl=en&source=hp&q=Linq+to+xml+complex&pbx=1&oq=Linq+to+xml+complex&aq=f&aqi=g2g-v2g-b1&aql=&gs_sm=e&gs_upl=446775l448585l2l449411l8l7l0l0l0l0l592l1591l4-1.2l3l0&bav=on.2,or.r_gc.r_pw.&fp=1bc11de366406738&biw=1280&bih=847
0
 

Author Comment

by:wlevy
ID: 36531452
Neither of the above solutions will work. Did you test them?
0
 
LVL 4

Accepted Solution

by:
guramrit earned 2000 total points
ID: 36535232
It'll work, check this...
 
XDocument xmlDoc = XDocument.Load("resources\\data.xml"); //Load method to load a xml file
            var q = (from c in xmlDoc.Descendants("Item")
                     select new Item()
                                {
                                    Name = c.Attribute("Name").Value,
                                    Color = c.Attribute("Color").Value,
                                    Pets = (from v in c.Elements("Pet")
                                            select new Pet()
                                                       {
                                                           Type = v.Attribute("Type").Value,
                                                           Age = Convert.ToInt32(v.Attribute("Age").Value)
                                                       }).ToList()

                                }).ToArray();

Open in new window


Change "resources\\data.xml" to your file path.
0
 

Author Closing Comment

by:wlevy
ID: 36536166
Perfect! Exactly what I needed. Thank you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question