DataTable or DataSet from XML

J

JohnAD

Hello NG,

Originally I posted this question on microsoft.public.xml but did not get
any reply all day so cross posting it on this NG in hope of getting some
answer -

I have a very basic understanding of XML so need some help here. I have a
XML file (structure given below). I want to change it to a DataSet or
DataTable. Basically I will be using a .Net language (do not matter which
one) and I have heard that there are lots of big support for XML in .Net.

Any pointers will be great.

Thank you very much.
Po



<?xml...
<?xml-stylesheet...

<rootNode attributes="...">
<SecondLevelRoot>
<PageTitle>I am a cool page title</PageTitle>
<Items>
<more1><![CDATA[hello]]></more1>
<more2>23423234234233223</more2>
<more3>lots of stuff....</more3>
<more4>more stuff.......</more4>
<more5>some url links...</more5>
</Items>
<Items>
.....
</Items>
<Items>
.....
</Items>
</SecondLevelRoot>
</rootNode>
 
J

JohnAD

This article gives me some ideas. I was wondering if rather creating
DataTable directly we could change the node names to make it more user
friendly? Is there a way to walk through every node name and change its name
or change while reading it into a DataTable or DataSet?
 
S

sloan

Ok.. a DataSet .. at is core is an xml file.
The advantage of a DataSet is that you get some MS methods for dealing with
that Xml.

There is also something called a "strong typed dataset", where you define
the schema for the xml/dataset. This is a powerful tool in DotNet.

You kinda have two options. Well, at least how I'd approach it.

1.
Define a strong typed dataset. ( Go to "Add New Item / DataSet")
You can name it whatever you want. I might call it ItemDS.

By defining a strong dataset, you can actually get to all the tables in the
dataset, all the row, and all the columns programatically.

Lets say in the ItemsDS, you create a table called "Item". And you add the
rows MoreStuff1 , MoreStuff2 (as strings)

You could then do

ItemDS ds = new ItemDS();

ItemDS.ItemRow newRow = ds.Item.NewItemRow();
newRow.MoreStuff1 = "abc";
newRow.MoreStuff2 = "def";
ds.Item.AddNewItemRow(newRow);

newRow = ds.Item.NewItemRow();
newRow.MoreStuff1 = "wxz";
newRow.MoreStuff2 = "tuv";
ds.Item.AddNewItemRow(newRow);

If you ran

Console.WriteLine (ds.GetXml()) ; //you'd get

<ItemDS>
<Item>
<MoreStuff1>abc</MoreStuff1>
<MoreStuff2>def</MoreStuff2>
</Item>
<Item>
<MoreStuff1>wxz</MoreStuff1>
<MoreStuff2>tuv</MoreStuff2>
</Item>
</ItemDS>


You'll have to play with it some......... you won't get it over night.

2.

You can create a strong typed dataset, (as above), and then do an Xml to Xml
transformation.

You would basically define an .xsl file, which is used to transform xml into
something else. Most times you change xml into HTML, but you can also
change it into another xml string.

That's alot of to go over. But its an idea. Your goal would be to create
an xml string that looked exactly like (structure wise) to

<ItemDS>
<Item>
<MoreStuff1>abc</MoreStuff1>
<MoreStuff2>def</MoreStuff2>
</Item>
<Item>
<MoreStuff1>wxz</MoreStuff1>
<MoreStuff2>tuv</MoreStuff2>
</Item>
</ItemDS>

and then you could load that up into a new ItemDS.



JohnAD said:
Hello NG,

Originally I posted this question on microsoft.public.xml but did not get
any reply all day so cross posting it on this NG in hope of getting some
answer -

I have a very basic understanding of XML so need some help here. I have a
XML file (structure given below). I want to change it to a DataSet or
DataTable. Basically I will be using a .Net language (do not matter which
one) and I have heard that there are lots of big support for XML in .Net.

Any pointers will be great.

Thank you very much.
Po



<?xml...
<?xml-stylesheet...

<rootNode attributes="...">
<SecondLevelRoot>
<PageTitle>I am a cool page title</PageTitle>
<Items>
<more1><![CDATA[hello]]></more1>
<more2>23423234234233223</more2>
<more3>lots of stuff....</more3>
<more4>more stuff.......</more4>
<more5>some url links...</more5>
</Items>
<Items>
.....
</Items>
<Items>
.....
</Items>
</SecondLevelRoot>
</rootNode>
 
J

JohnAD

Thanks sloan that is a good information. I have started working on xml books
for now :)



sloan said:
Ok.. a DataSet .. at is core is an xml file.
The advantage of a DataSet is that you get some MS methods for dealing
with
that Xml.

There is also something called a "strong typed dataset", where you define
the schema for the xml/dataset. This is a powerful tool in DotNet.

You kinda have two options. Well, at least how I'd approach it.

1.
Define a strong typed dataset. ( Go to "Add New Item / DataSet")
You can name it whatever you want. I might call it ItemDS.

By defining a strong dataset, you can actually get to all the tables in
the
dataset, all the row, and all the columns programatically.

Lets say in the ItemsDS, you create a table called "Item". And you add
the
rows MoreStuff1 , MoreStuff2 (as strings)

You could then do

ItemDS ds = new ItemDS();

ItemDS.ItemRow newRow = ds.Item.NewItemRow();
newRow.MoreStuff1 = "abc";
newRow.MoreStuff2 = "def";
ds.Item.AddNewItemRow(newRow);

newRow = ds.Item.NewItemRow();
newRow.MoreStuff1 = "wxz";
newRow.MoreStuff2 = "tuv";
ds.Item.AddNewItemRow(newRow);

If you ran

Console.WriteLine (ds.GetXml()) ; //you'd get

<ItemDS>
<Item>
<MoreStuff1>abc</MoreStuff1>
<MoreStuff2>def</MoreStuff2>
</Item>
<Item>
<MoreStuff1>wxz</MoreStuff1>
<MoreStuff2>tuv</MoreStuff2>
</Item>
</ItemDS>


You'll have to play with it some......... you won't get it over night.

2.

You can create a strong typed dataset, (as above), and then do an Xml to
Xml
transformation.

You would basically define an .xsl file, which is used to transform xml
into
something else. Most times you change xml into HTML, but you can also
change it into another xml string.

That's alot of to go over. But its an idea. Your goal would be to create
an xml string that looked exactly like (structure wise) to

<ItemDS>
<Item>
<MoreStuff1>abc</MoreStuff1>
<MoreStuff2>def</MoreStuff2>
</Item>
<Item>
<MoreStuff1>wxz</MoreStuff1>
<MoreStuff2>tuv</MoreStuff2>
</Item>
</ItemDS>

and then you could load that up into a new ItemDS.



JohnAD said:
Hello NG,

Originally I posted this question on microsoft.public.xml but did not get
any reply all day so cross posting it on this NG in hope of getting some
answer -

I have a very basic understanding of XML so need some help here. I have a
XML file (structure given below). I want to change it to a DataSet or
DataTable. Basically I will be using a .Net language (do not matter which
one) and I have heard that there are lots of big support for XML in .Net.

Any pointers will be great.

Thank you very much.
Po



<?xml...
<?xml-stylesheet...

<rootNode attributes="...">
<SecondLevelRoot>
<PageTitle>I am a cool page title</PageTitle>
<Items>
<more1><![CDATA[hello]]></more1>
<more2>23423234234233223</more2>
<more3>lots of stuff....</more3>
<more4>more stuff.......</more4>
<more5>some url links...</more5>
</Items>
<Items>
.....
</Items>
<Items>
.....
</Items>
</SecondLevelRoot>
</rootNode>
 

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