Get Child Nodes

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am using Linq To XML to get data from a XML file and populate an
object:

// Define product
Product product = (from p in XElement.Load("../Data/
Products.xml").Elements("Products");
where p.Element("Id").Value == id.ToString
()
select new Product {
Id = new Guid(p.Element("Id").Value),
Brochure = p.Element("Brochure").Value,
Created = DateTime.Parse(p.Element("Created").Value),
Image = p.Element("Image").Value,
Name = p.Element("Name").Value,
Updated = DateTime.Parse(p.Element("Updated").Value),
Supplier = new Supplier {
// ???????????????????????????????????????
}
}).SingleOrDefault();

How can I get the data from the Supplier?

My XML file is as follows:

<Products>
<Product>
<Id></Id>
<Brochure></Brochure>
<Created></Created>
<Image></Image>
<Name></Name>
<Updated></Updated>
<Supplier>
<Id></Id>
<Created></Created>
<Name></Name>
<Updated></Updated>
</Supplier>
</Product>
</Products>

Thank You
 
shapper said:
Hello,

I am using Linq To XML to get data from a XML file and populate an
object:

// Define product
Product product = (from p in XElement.Load("../Data/
Products.xml").Elements("Products");
where p.Element("Id").Value == id.ToString
()
select new Product {
Id = new Guid(p.Element("Id").Value),
Brochure = p.Element("Brochure").Value,
Created = DateTime.Parse(p.Element("Created").Value),
Image = p.Element("Image").Value,
Name = p.Element("Name").Value,
Updated = DateTime.Parse(p.Element("Updated").Value),
Supplier = new Supplier()
}
}).SingleOrDefault();

How can I get the data from the Supplier?


I don't know, maybe after you have shapped Product. Maybe something like
this.

Product.Supplier = (from a in Shippers select a).SingleOrDefault();



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4136 (20090606) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
Pretty much the same way you get data from any of the other elements.  Can  
you be more specific about your question?

Hi Pete,

Yes I have:
// ...
Updated = DateTime.Parse(p.Element("Updated").Value),
Supplier = new Supplier {
Id = p.Element("Supplier"). ????
Name = p.Element("Supplier"). ????
}

I am confused on how to get the Supplier child elements to fill the
Supplier object.
 
Pretty much the same way you get data from any of the other elements. Can
you be more specific about your question?

Hi Pete,

Yes I have:
// ...
Updated = DateTime.Parse(p.Element("Updated").Value),
Supplier = new Supplier {
Id = p.Element("Supplier"). ????
Name = p.Element("Supplier"). ????
}


'p' is an object at that point. So why can't you call a method that passes
'p' to the method and return Supplier out of it?


Supplier = new Supplier,
Supplier = GetSupplier(p)


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4136 (20090606) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
Back
Top