Add Child Node after GetXML

W

Workaholic

Hi,

I have a test XML file, as follows:

<MAIN>
<CUSTOMER>
<NAME>First Customer</NAME>
</CUSTOMER>
</MAIN>

I am using the dataset.GetXML method to read this into VB.Net, as
follows:

Dim xmlreader As New DataSet
xmlreader.ReadXml("c:\temp\test.xml")

I want to add a child node to the XML, and have written the following
code:

xmlreader.Tables.Add("ORDERS")
xmlreader.Tables("ORDERS").Columns.Add("ORDER_NO")
xmlreader.Tables("ORDERS").Rows.Add(xmlreader.Tables
("ORDERS").NewRow)
xmlreader.Tables("ORDERS").Rows(0).Item("ORDER_NO") = 1

However, the new node is always added to the end of the XML, between
the </CUSTOMER> and </MAIN> lines

Please can somebody tell me how to amend my VB.Net code to add this
structure as a nested node under the CUSTOMER record?

Many thanks
 
T

Teemu

Workaholic said:
Hi,

I have a test XML file, as follows:

<MAIN>
<CUSTOMER>
<NAME>First Customer</NAME>
</CUSTOMER>
</MAIN>

I am using the dataset.GetXML method to read this into VB.Net, as
follows:

Dim xmlreader As New DataSet
xmlreader.ReadXml("c:\temp\test.xml")

However, the new node is always added to the end of the XML, between
the </CUSTOMER> and </MAIN> lines

Please can somebody tell me how to amend my VB.Net code to add this
structure as a nested node under the CUSTOMER record?

You are using DataSet and it doesn't support nested tables. Is there a
particular reason why you handle XML with DataSet? If load your XML using
XMLDocument object then it's very easy to add nodes where you want.

-Teemu
 
T

Teemu

Please can somebody tell me how to amend my VB.Net code to add this
structure as a nested node under the CUSTOMER record?

Here is something to start with:

Dim Document As New Xml.XmlDocument
Document.Load("d:\vb.xml")

Document.SelectSingleNode("/MAIN/CUSTOMER").AppendChild(Document.CreateElement("ORDERS"))

Dim NewOrder As Xml.XmlElement = Document.CreateElement("ORDER_NO")
NewOrder.AppendChild(Document.CreateTextNode("1"))

Document.SelectSingleNode("/MAIN/CUSTOMER/ORDERS").AppendChild(NewOrder)

MsgBox(Document.OuterXml)
Document.Save("d:\new.xml")

-Teemu
 
W

Workaholic

Hi Teemu,

Thanks for your response. There's no particular reason why I am using
a dataset, but my application will require a lot of re-writing if I
change to an XMLDocument. My code snippet is obviously just an
example, and the real application (and associated XML) is much more
complex. I have found the dataset object works well for the
application, as it is easy to query the tables using syntax such as
xmlreader.tables("CUSTOMER").rows(0).item("ORDER").

Is there absolutely no way to add a child node to a specific row using
the dataset object? I would really prefer to do this than have to re-
write (and re-test) a very large and complex application.

Many thanks for your help
 
T

Teemu

I have found the dataset object works well for the
application, as it is easy to query the tables using syntax such as
xmlreader.tables("CUSTOMER").rows(0).item("ORDER").

In my opinion it is equally easy to get data from XML using XPath
expressions. It is possible to create quite complex queries if you look how
XPath works.
Is there absolutely no way to add a child node to a specific row using
the dataset object? I would really prefer to do this than have to re-
write (and re-test) a very large and complex application.

I don't know any way to do this but maybe somebody knows a trick to do it if
it's possible.

-Teemu
 
T

Teemu

I don't know any way to do this but maybe somebody knows a trick to do it
if it's possible.

Well, here is one trick to do it. You can't create nested tables but you can
make relations between tables like in real databases. This means that you
have to create unique IDs and then you can link a customer to its orders.

Try to read this XML:

<MAIN>
<CUSTOMER>
<NAME>First Customer</NAME>
<ITEMS>
<ITEM>Car</ITEM>
</ITEMS>
</CUSTOMER>
<CUSTOMER>
<NAME>Second Customer</NAME>
<ITEMS>
<ITEM>Bike</ITEM>
</ITEMS>
</CUSTOMER>
</MAIN>

You can see that in DataSet there are two tables and VB has created IDs for
customers. In DataSet there is one relation, take a look at
DataSet.Relations(0) and figure out how it works.

This is the way you can create a nested XML file.

-Teemu
 
F

Family Tree Mike

Workaholic said:
Hi,

I have a test XML file, as follows:

<MAIN>
<CUSTOMER>
<NAME>First Customer</NAME>
</CUSTOMER>
</MAIN>

I am using the dataset.GetXML method to read this into VB.Net, as
follows:

Dim xmlreader As New DataSet
xmlreader.ReadXml("c:\temp\test.xml")

I want to add a child node to the XML, and have written the following
code:

xmlreader.Tables.Add("ORDERS")
xmlreader.Tables("ORDERS").Columns.Add("ORDER_NO")
xmlreader.Tables("ORDERS").Rows.Add(xmlreader.Tables
("ORDERS").NewRow)
xmlreader.Tables("ORDERS").Rows(0).Item("ORDER_NO") = 1

However, the new node is always added to the end of the XML, between
the </CUSTOMER> and </MAIN> lines

Please can somebody tell me how to amend my VB.Net code to add this
structure as a nested node under the CUSTOMER record?

Many thanks


This works, though I don't know if keeping the data in the first table works
for your overall requirements. This puts the data in the first table. If
your requirement is storage of xml in a particular format, then I agree with
Teemu, that using XML from the start would have been better. If your design
is such that you depend on tables in a particular layout, but have a
requirement to go outbound to XML, then you should look at customizing the
serialization. The various XML classes will make this easy.

xmlreader.Tables("CUSTOMER").Columns.Add("ORDER_NO")
xmlreader.Tables("CUSTOMER").Rows(0).Item("ORDER_NO") = 1
 

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