How to Append Node in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sean Len
    New Member
    • Oct 2010
    • 13

    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:
    Code:
    <?xml version="1.0"?>
    <ReportModule>
      <Client Client="Client1">
        <ReportTitle Title="Client1 Monthly Report">
          <Config>C:\\Client1 Monthly Report.xml</Config>
        </ReportTitle>
      </Client>
      <Client Client="Client2">
        <ReportTitle Title="Client2 Monthly Report">
          <Config>C:\\Client2 Monthly Report.xml</Config>
        </ReportTitle>
      </Client>
    </ReportModule>
    Any idea how to append a new ReportTitle record under Client1 to achieve below?
    Changes is at Line 7 to 9.
    Code:
    <?xml version="1.0"?>
    <ReportModule>
      <Client Client="Client1">
        <ReportTitle Title="Client1 Monthly Report">
          <Config>C:\\Client1 Monthly Report.xml</Config>
        </ReportTitle>
    	<ReportTitle Title="Client1 Monthly Report 2">
          <Config>C:\\Client1 Monthly Report 2.xml</Config>
        </ReportTitle>
      </Client>
      <Client Client="Client2">
        <ReportTitle Title="Client2 Monthly Report">
          <Config>C:\\Client2 Monthly Report.xml</Config>
        </ReportTitle>
      </Client>
    </ReportModule>
    Anyone have experience or sample about this situation in C#?
    Thank you very much.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    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

    Comment

    • Sean Len
      New Member
      • Oct 2010
      • 13

      #3
      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.
      Code:
      private void AddNewNode()
      {
          XmlDocument xmlDoc = new XmlDocument();
          xmlDoc.LoadXml("C://XML.xml");
      
          // declare a fragment to contain the to-be added new childnode
          XmlDocumentFragment xmlDocFrag = xmlDoc.CreateDocumentFragment();
                  
          // Construct the new Xml node that you want to insert
          xmlDocFrag.InnerXml = @"<ReportTitle Title='Client1_2 Monthly Report'>" +
                    "<Config>C:\\Client1_2 Monthly Report.xml</Config>" +
                    "</ReportTitle>";
      
          // for each loop to find the target locatin to add the new childnode
          foreach (XmlNode xmlNode in xmlDoc)
          {
              // make sure it is ReportModule tag
              if (xmlNode.Name == "ReportModule")
              {
                  // Another for each loop to target desire Child node
                  foreach (XmlNode xmlNodeChild in xmlNode.ChildNodes)
                  {
                      // try to find the node with Client tag
                      if (xmlNodeChild.Name == "Client")
                      {
                          // check if the Client tag is the one we want to
                          // append/insert our new node.
                          if (xmlNodeChild.Attributes["Client"].Value == "Client1")
                          {
                              // append the new fragment to the found child node
                              xmlNodeChild.AppendChild(xmlDocFrag);
                              break;
                          }
                      }
                  }
              }
          }
      }
      Thanks to Mike Wong who implement the solution.

      Comment

      Working...