Populating objects from XML file

A

Adam

Hello, I have an XML file that contains parent / child relations
between objects.

e.g.

There is a Manager who can have one to many employees and these
employees can have one to many phone numbers.

I would like to know the best way to retrieve this information from
the XML file and store into business objects.

I have the following idea which would work

1) Load the XML file into a DataSet (After validating against a
schema) and then use the relationships defined in the schema to match
objects between datatables

Is there a better / way to do this?

Thanks
Adam
 
P

Peter Duniho

Adam said:
Hello, I have an XML file that contains parent / child relations
between objects.

[...]
I have the following idea which would work

1) Load the XML file into a DataSet (After validating against a
schema) and then use the relationships defined in the schema to match
objects between datatables

Is there a better / way to do this?

Unless you have a specific need to use database-like operations and
logic on the data, I don't think the DataSet is the best way, simply
because it's necessarily a fundamentally flat data structure from which
the tree data structure will have to be reconstructed. That is, there's
no problem representing a tree data structure in a database, but IMHO
it's not the most natural way to do it.

That said, without knowing more about the actual data structures you're
trying to create, it's hard to offer specific advice. But I can tell
you that for my own purposes, I've found it convenient to simply write a
layer of code that can map to and from my own data object types and the
XML types in System.Xml.Linq, loading the XML file as an XDocument and
then traversing the XDocument structure, reconstructing the object
hierarchy in my own data types as I go.

Pete
 
A

Adam

Adam said:
Hello, I have an XML file that contains parent / child relations
between objects.
[...]
I have the following idea which would work
1) Load the XML file into a DataSet (After validating against a
schema) and then use the relationships defined in the schema to match
objects between datatables
Is there a better / way to do this?

Unless you have a specific need to use database-like operations and
logic on the data, I don't think the DataSet is the best way, simply
because it's necessarily a fundamentally flat data structure from which
the tree data structure will have to be reconstructed.  That is, there's
no problem representing a tree data structure in a database, but IMHO
it's not the most natural way to do it.

That said, without knowing more about the actual data structures you're
trying to create, it's hard to offer specific advice.  But I can tell
you that for my own purposes, I've found it convenient to simply write a
layer of code that can map to and from my own data object types and the
XML types in System.Xml.Linq, loading the XML file as an XDocument and
then traversing the XDocument structure, reconstructing the object
hierarchy in my own data types as I go.

Pete

Thanks for your help Pete :)
 
M

Mr. Arnold

Adam said:
Hello, I have an XML file that contains parent / child relations
between objects.

e.g.

There is a Manager who can have one to many employees and these
employees can have one to many phone numbers.

I would like to know the best way to retrieve this information from
the XML file and store into business objects.

I have the following idea which would work

1) Load the XML file into a DataSet (After validating against a
schema) and then use the relationships defined in the schema to match
objects between datatables

Is there a better / way to do this?

You can use Linq-2-XML which does work with relationships in XML by
using the Join and Advanced Query techniques. I use Linq to go get a XML
template of data on a fileshare.

I use template of XML data to complete a business object before I pass
the object to the DAL to persist the object to the data stor.

<http://msdn.microsoft.com/en-us/library/bb669130.aspx>
 
A

Arne Vajhøj

Hello, I have an XML file that contains parent / child relations
between objects.

e.g.

There is a Manager who can have one to many employees and these
employees can have one to many phone numbers.

I would like to know the best way to retrieve this information from
the XML file and store into business objects.

I have the following idea which would work

1) Load the XML file into a DataSet (After validating against a
schema) and then use the relationships defined in the schema to match
objects between datatables

Is there a better / way to do this?

First thing to try is to generate classes from the XML
schema with the XSD utility.

If it works then you can load very easy.

Arne
 

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