473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Add to an XML node

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!
Jul 17 '08 #1
4 1780
wc*********@gmail.com wrote:
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/
Jul 17 '08 #2
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?
Jul 17 '08 #3
wcmcalister wrote:
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/
Jul 17 '08 #4
Thank you Martin. I have a lot learn about working with XML in dot Net.

Have a wonderful day (I know I am)!
Jul 17 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: J Peterman | last post by:
Im having a nightmare trying to understand these nodes and linked lists. I've posted my code for my node.h, node.cpp, linkedlist.h and linkedlist.cpp files in separates replies. Can someone...
2
by: Ralf Wahner | last post by:
Dear Masters of XSLT Could I ask you for a clue on the following question? I'd like to use XSLT to transform an XML source file to LaTeX. In the following small example the <para> Element...
6
by: Chuck | last post by:
What's the difference between using "catalog node" and catalog admin node"? -- Chuck Remove "_nospam" to reply by email
3
by: stabbert | last post by:
We are running DB2 UDB 8.1.6 in a partitioned environment where we have 8 physical nodes. We have a process that remotely connects to each individual node and loads some data. By setting the...
5
by: Jeroen Ceuppens | last post by:
I need to put a new node at the end of the tree, that end is not te lowest in de list but the deepest (the one with the most + before it) Node A Node 1 Node 2 Node 3 Node 4: Deepest Node B:...
3
by: Saradhi | last post by:
Hi All, Here I am facing a performance problem with the TreeView Node renaming. I am displaying a hierarchy Data in a treeview in my Windows C# Application. My tree view represents an...
8
by: Dylan Parry | last post by:
Hi folks, I have an XML node called "myNode" and it contains: "This is some text" Now I can use the myNode.nodeValue property to get the string of text above. But say myNode contains: ...
6
by: Derek Hart | last post by:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I run xpath statements against the xml file to grab data from it, so I use, as an example, //Vehicles/Vehicles/@make to get...
9
by: Moses | last post by:
Hi All, How to check weather a node has sibling? Is there any function like " hasChildNodes() " Thanks in Advance Moses
3
by: Kane | last post by:
When you create node 1 you allocate memory and link it Again when you create node 2 you would allocate memory for that node in a different section of the code. Is there more efficient way where I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.