Connecting Tech Pros Worldwide Forums | Help | Site Map

Add to an XML node

wcmcalister@gmail.com
Guest
 
Posts: n/a
#1: Jul 17 '08
I have a web service that returns me an XmlNode ojbect. Here is an
example of the outerXML:
<?xml version="1.0" encoding="utf-8" ?>
<NewDataSet>
<Table>
<ID>123</ID>
<TheName>Bob<TheName>
</Table
<Table>
<ID>124</ID>
<TheName>Barry<TheName>
</Table
<Table>
<ID>125</ID>
<TheName>Beth<TheName>
</Table
<Table>
<ID>126</ID>
<TheName>Beatrix<TheName>
</Table
<Table>
<ID>127</ID>
<TheName>Benjamin<TheName>
</Table
<Table>
<ID>128</ID>
<TheName>Betty<TheName>
</Table
</NewDataSet>

I also have a list of IDs with some AccountNumbers that looks like
this:
ID AccountNumber
123 987654321
124 987654322
125 987654323
126 987654324
127 987654325
128 987654326

I would like to match this info into the XML so that it looks like
this:
<?xml version="1.0" encoding="utf-8" ?>
<NewDataSet>
<Table>
<ID>123</ID>
<TheName>Bob<TheName>
<AccountNum>987654321</AccountNum>
</Table
<Table>
<ID>124</ID>
<TheName>Barry<TheName>
<AccountNum>987654322</AccountNum>
</Table
<Table>
<ID>125</ID>
<TheName>Beth<TheName>
<AccountNum>987654323</AccountNum>
</Table
<Table>
<ID>126</ID>
<TheName>Beatrix<TheName>
<AccountNum>987654324</AccountNum>
</Table
<Table>
<ID>127</ID>
<TheName>Benjamin<TheName>
<AccountNum>987654325</AccountNum>
</Table
<Table>
<ID>128</ID>
<TheName>Betty<TheName>
<AccountNum>987654326</AccountNum>
</Table
</NewDataSet>

Can someone show me how to match the data into the XmlNode object?

Thanks!

Martin Honnen
Guest
 
Posts: n/a
#2: Jul 17 '08

re: Add to an XML node


wcmcalister@gmail.com wrote:
Quote:
Can someone show me how to match the data into the XmlNode object?
Use XPath to find the right Table element, then use CreateElement to
create a new AccountNum element, then use AppendChild to add the newly
created element. When finished, save the XML document:

XmlDocument doc = new XmlDocument();
doc.Load(@"..\..\XMLFile1.xml");

List<Dataaccounts = new List<Data>();
Data account = new Data();
account.ID = 123;
account.AccountNumber = 987654321;
accounts.Add(account);
account = new Data();
account.ID = 124;
account.AccountNumber = 987654322;
accounts.Add(account);

foreach (Data data in accounts)
{
XmlNode table =
doc.SelectSingleNode(string.Format("/NewDataSet/Table[ID = {0}]", data.ID));
if (table != null)
{
XmlElement accountNum =
doc.CreateElement("AccountNum");
accountNum.InnerText = data.AccountNumber.ToString();
table.AppendChild(accountNum);
}
}
//saving to Console.Out for testing, could save to file or
stream instead
doc.Save(Console.Out);



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
=?Utf-8?B?d2NtY2FsaXN0ZXI=?=
Guest
 
Posts: n/a
#3: Jul 17 '08

re: Add to an XML node


Thanks so much Martin. I can now finish my project on time.

I don't absolutely need this but is there an easy way to convert the
XmlDocument back to an XmlNode?
Martin Honnen
Guest
 
Posts: n/a
#4: Jul 17 '08

re: Add to an XML node


wcmcalister wrote:
Quote:
Thanks so much Martin. I can now finish my project on time.
>
I don't absolutely need this but is there an easy way to convert the
XmlDocument back to an XmlNode?
XmlDocument is a subclass of XmlNode so any XmlDocument instance is also
an instance of XmlNode. So there is no need to do any conversion.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
=?Utf-8?B?d2NtY2FsaXN0ZXI=?=
Guest
 
Posts: n/a
#5: Jul 17 '08

re: Add to an XML node


Thank you Martin. I have a lot learn about working with XML in dot Net.

Have a wonderful day (I know I am)!
Closed Thread