473,387 Members | 1,585 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,387 software developers and data experts.

How to Append Node in C#?

Hello all, I currently working on a program that allow to append new xml node into a existing xml doc.
The following is my senario:
I need to generate few report based on 2 different client, Client1 and Client2.
Each client have 1 report title and config tag.
How can I add 1 more report title and config tag under Client1?

Currently I have a XML contain the following info:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <ReportModule>
  3.   <Client Client="Client1">
  4.     <ReportTitle Title="Client1 Monthly Report">
  5.       <Config>C:\\Client1 Monthly Report.xml</Config>
  6.     </ReportTitle>
  7.   </Client>
  8.   <Client Client="Client2">
  9.     <ReportTitle Title="Client2 Monthly Report">
  10.       <Config>C:\\Client2 Monthly Report.xml</Config>
  11.     </ReportTitle>
  12.   </Client>
  13. </ReportModule>
Any idea how to append a new ReportTitle record under Client1 to achieve below?
Changes is at Line 7 to 9.
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <ReportModule>
  3.   <Client Client="Client1">
  4.     <ReportTitle Title="Client1 Monthly Report">
  5.       <Config>C:\\Client1 Monthly Report.xml</Config>
  6.     </ReportTitle>
  7.     <ReportTitle Title="Client1 Monthly Report 2">
  8.       <Config>C:\\Client1 Monthly Report 2.xml</Config>
  9.     </ReportTitle>
  10.   </Client>
  11.   <Client Client="Client2">
  12.     <ReportTitle Title="Client2 Monthly Report">
  13.       <Config>C:\\Client2 Monthly Report.xml</Config>
  14.     </ReportTitle>
  15.   </Client>
  16. </ReportModule>
Anyone have experience or sample about this situation in C#?
Thank you very much.
Nov 30 '10 #1
2 3102
jkmyoung
2,057 Expert 2GB
What clasess are you using to read, and write the xml?
Are you using XmlDocument objects?

Do you know the values of all your nodes before you create it, or do you have to read the values of them beforehand?
Examples of creating nodes: http://msdn.microsoft.com/en-us/library/fw1ys7w6.aspx
Dec 2 '10 #2
Hi jk,
Ya I am using XmlDocument to read an XML.
I already found answer for this issues.
Below is my solution. Just to share out with someone out there with similar problem.
Expand|Select|Wrap|Line Numbers
  1. private void AddNewNode()
  2. {
  3.     XmlDocument xmlDoc = new XmlDocument();
  4.     xmlDoc.LoadXml("C://XML.xml");
  5.  
  6.     // declare a fragment to contain the to-be added new childnode
  7.     XmlDocumentFragment xmlDocFrag = xmlDoc.CreateDocumentFragment();
  8.  
  9.     // Construct the new Xml node that you want to insert
  10.     xmlDocFrag.InnerXml = @"<ReportTitle Title='Client1_2 Monthly Report'>" +
  11.               "<Config>C:\\Client1_2 Monthly Report.xml</Config>" +
  12.               "</ReportTitle>";
  13.  
  14.     // for each loop to find the target locatin to add the new childnode
  15.     foreach (XmlNode xmlNode in xmlDoc)
  16.     {
  17.         // make sure it is ReportModule tag
  18.         if (xmlNode.Name == "ReportModule")
  19.         {
  20.             // Another for each loop to target desire Child node
  21.             foreach (XmlNode xmlNodeChild in xmlNode.ChildNodes)
  22.             {
  23.                 // try to find the node with Client tag
  24.                 if (xmlNodeChild.Name == "Client")
  25.                 {
  26.                     // check if the Client tag is the one we want to
  27.                     // append/insert our new node.
  28.                     if (xmlNodeChild.Attributes["Client"].Value == "Client1")
  29.                     {
  30.                         // append the new fragment to the found child node
  31.                         xmlNodeChild.AppendChild(xmlDocFrag);
  32.                         break;
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
  39.  
Thanks to Mike Wong who implement the solution.
Dec 3 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: DrewM | last post by:
Note: I'm not using the xmldom functions because I need to maintain compatibility with current typical installs. Say I have an XML file like this on my disk: <list> <item>foo</item>...
3
by: J Krugman | last post by:
This question refers to the DOM. I would like to dynamically append some small amount of text to the all-text content of a <pre> node. I suppose that I could extract the existing content, and...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
4
by: Grant | last post by:
Gday, I am using the sample code at: http://support.microsoft.com/default.aspx?scid=kb;en-us;317666 to modify an existing XML document. The XML document looks like the following: ...
4
by: glbdev | last post by:
Hi, I posted this in "microsoft.public.xml.msxml-webrelease" but now realize it should probably have been in the ASP group. Sorry if that causes any problems. I have an XML document like:...
2
by: Freddy Coal | last post by:
Hi, I would like append a new node with elements in a existing XML file, how make that?. For example: <?xml version="1.0" encoding="utf-8" ?> - <Configuracion> - <Estaciones>...
1
by: Aswanth | last post by:
I'm Using Asp.Net with C#.. & Using XML Files.. Please check this.. <Page_Details> <Page="HomePage.aspx" Time="10:30:20:32636" Type="S" ID = "1" Category_ID="41" /> <Page="HomePage.aspx"...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i...
1
by: Greg Jackson | last post by:
Hello, I am having some trouble in working out the distance of each node from the starting node in my Dijkstra Algorithm code. Here is my code with the section i'm stuck on in bold: infinity =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.