Add row to (xml) dataset

  • Thread starter Thread starter Gene Vangampelaere
  • Start date Start date
G

Gene Vangampelaere

Hi,

I'm having a frustrating problem :)
I have an XML like this :

<?xml version="1.0" encoding="utf-8"?>
<cgb>
<parameters>
<parameter>
<paramName>patientNr</paramName>
<paramValue>7507244765</paramValue>
</parameter>
<parameter>
<paramName>ApplicationKey</paramName>
<paramValue>cpd</paramValue>
</parameter>
</parameters>
<!-- here-->
</cgb>


and I want to add 2 more "parameter" rows. When I use my function the
parameters are added where I put the comment line.

My function :

Dim ds As New DataSet
Dim reader As XmlReader = New XmlTextReader(New StringReader(xmlParameters))
ds.ReadXml(reader)
Dim row As DataRow
row = ds.Tables("parameter").NewRow
row("paramName") = paramName
row("paramValue") = paramValue
ds.Tables("parameter").Rows.Add(row)
Return ds.GetXml


What am I doing wrong ? I want my xml like this


<cgb>
<parameters>
<parameter>
<paramName>patientNr</paramName>
<paramValue>7507244765</paramValue>
</parameter>
<parameter>
<paramName>ApplicationKey</paramName>
<paramValue>cpd</paramValue>
</parameter>

<parameter>
<paramName>GUID</paramName>
<paramValue>84BA0FFC-2BE2-401f-A294-EFB00CA3A3A9</paramValue>
</parameter>
<parameter>
<paramName>SID</paramName>
<paramValue>01050000000000051500000071459C02367CDD12DA1B396F41090000</paramValue>
</parameter>

</parameters>

</cgb>
 
You need to set the parent row for your newly added row. Your dataset has
two tables, named "parameters" and "parameter." The "parameters" table has
one row (row 0). So, first you get a reference to that row, and then set
that as the parent for your newly created "parameter" row. Note that you
should add the relationship before adding the child row. For example:

Dim ds As New DataSet
Dim reader As XmlReader = New XmlTextReader(New StringReader(xmlParameters))
ds.ReadXml(reader)

Dim rowParent As DataRow = ds.Tables("parameters").Rows(0)
Dim row As DataRow
row = ds.Tables("parameter").NewRow
row("paramName") = paramName
row("paramValue") = paramValue
row.SetParent(rowParent)
ds.Tables("parameter").Rows.Add(row)
Return ds.GetXml

Now the output should look like you want it to.
 
thx !



Russell Jones said:
You need to set the parent row for your newly added row. Your dataset has
two tables, named "parameters" and "parameter." The "parameters" table has
one row (row 0). So, first you get a reference to that row, and then set
that as the parent for your newly created "parameter" row. Note that you
should add the relationship before adding the child row. For example:

Dim ds As New DataSet
Dim reader As XmlReader = New XmlTextReader(New
StringReader(xmlParameters))
ds.ReadXml(reader)

Dim rowParent As DataRow = ds.Tables("parameters").Rows(0)
Dim row As DataRow
row = ds.Tables("parameter").NewRow
row("paramName") = paramName
row("paramValue") = paramValue
row.SetParent(rowParent)
ds.Tables("parameter").Rows.Add(row)
Return ds.GetXml

Now the output should look like you want it to.

"Gene Vangampelaere"
 
Back
Top