Get Child Nodes

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
 
M

Mr. Arnold

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
 
S

shapper

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.
 
M

Mr. Arnold

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top