Connecting Tech Pros Worldwide Help | Site Map

Add to an XML node

  #1  
Old July 17th, 2008, 01:15 PM
wcmcalister@gmail.com
Guest
 
Posts: n/a
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!
  #2  
Old July 17th, 2008, 01:35 PM
Martin Honnen
Guest
 
Posts: n/a

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/
  #3  
Old July 17th, 2008, 02:15 PM
=?Utf-8?B?d2NtY2FsaXN0ZXI=?=
Guest
 
Posts: n/a

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?
  #4  
Old July 17th, 2008, 02:25 PM
Martin Honnen
Guest
 
Posts: n/a

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/
  #5  
Old July 17th, 2008, 03:05 PM
=?Utf-8?B?d2NtY2FsaXN0ZXI=?=
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add xsi:nil="true" to an XML document node using vb.net jazzygirl answers 0 June 2nd, 2008 06:24 PM
Is it possible to delete an Xml node jibesh answers 1 February 10th, 2007 04:15 PM
Appending to an XML file. tony.collings@gmail.com answers 2 November 17th, 2005 11:47 AM
Exporting from XMLReader to an XML file Leszek answers 7 November 12th, 2005 02:28 AM