Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 7th, 2009, 09:25 AM
Authorized User
 
Join Date: Jul 2007
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default Adding a node to an existing xml node list.

Hi,
I am reading an xml document which has companies information like this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<Companies>
  <Company>
    <CustomerNo><![CDATA[67]]></CustomerNo>
    <CompanyName><![CDATA[ABC lead]]></CompanyName>
    <Address><![CDATA[888 homme way]]></Address>
    <Zip><![CDATA[94520]]></Zip>
    <City><![CDATA[San Jose]]></City>
    <State><![CDATA[California]]></State>
    <Country><![CDATA[USA]]></Country>

    <Phone><![CDATA[898-989-8988]]></Phone>
    <Fax><![CDATA[565-565-5655]]></Fax>
    <Email><![CDATA[jsmith@abc.com]]></Email>
    <Custom_2><![CDATA[]]></Custom_2>
    <Custom_5><![CDATA[]]></Custom_5>
    <CustomerTypeText2><![CDATA[DownloadPlus]]></CustomerTypeText2>
    <Custom_11><![CDATA[]]></Custom_11>
    <Custom_12><![CDATA[]]></Custom_12>
    <Custom_1><![CDATA[0]]></Custom_1>

    <Custom_3><![CDATA[]]></Custom_3>
    <Custom_9><![CDATA[]]></Custom_9>
    <Custom_8><![CDATA[]]></Custom_8>
    <Custom_6><![CDATA[]]></Custom_6>
    <Custom_7><![CDATA[]]></Custom_7>
    <Custom_4><![CDATA[]]></Custom_4>
    <Custom_10><![CDATA[]]></Custom_10>
  </Company>
<Comapny 2>
...
</Comapny 2>
</Companies>
I read each company information like so:

Code:
 private void ReadXmlData(string xmlSource)
        {
            XmlNodeList list = null;
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(xmlSource);
            list = xdoc.GetElementsByTagName("Company");
            foreach (XmlNode node in list)
            {
....
}
The variable 'node' gives me each company with it's child elements.
The thing that I am not able to do is add a new node to the 'node' variable.
For instance in the first run I have the following information in the 'node' variable.
Code:
<Company>
    <CustomerNo><![CDATA[67]]></CustomerNo>
    <CompanyName><![CDATA[ABC lead]]></CompanyName>
    <Address><![CDATA[888 homme way]]></Address>
    <Zip><![CDATA[94520]]></Zip>
    <City><![CDATA[San Jose]]></City>
    <State><![CDATA[California]]></State>
    <Country><![CDATA[USA]]></Country>

    <Phone><![CDATA[898-989-8988]]></Phone>
    <Fax><![CDATA[565-565-5655]]></Fax>
    <Email><![CDATA[jsmith@abc.com]]></Email>
    <Custom_2><![CDATA[]]></Custom_2>
    <Custom_5><![CDATA[]]></Custom_5>
    <CustomerTypeText2><![CDATA[DownloadPlus]]></CustomerTypeText2>
    <Custom_11><![CDATA[]]></Custom_11>
    <Custom_12><![CDATA[]]></Custom_12>
    <Custom_1><![CDATA[0]]></Custom_1>

    <Custom_3><![CDATA[]]></Custom_3>
    <Custom_9><![CDATA[]]></Custom_9>
    <Custom_8><![CDATA[]]></Custom_8>
    <Custom_6><![CDATA[]]></Custom_6>
    <Custom_7><![CDATA[]]></Custom_7>
    <Custom_4><![CDATA[]]></Custom_4>
    <Custom_10><![CDATA[]]></Custom_10>
  </Company>
To this I want to add a 'CustomerID' node.
How to go about it.
Any help is greatly appreciated.
 
Old October 7th, 2009, 09:37 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You'll want to create the new element using XmlDocument.CreateElement. Then you can use any of the numerous Insert/Append methods on the XmlNode to add it to the tree.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
codehelp (October 12th, 2009)
 
Old October 12th, 2009, 07:41 AM
Authorized User
 
Join Date: Jul 2007
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default Thanks

Thank you Sam.
It works.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a new Node to existing XML document Lupus81 XML 3 August 4th, 2009 04:57 PM
Problem adding a xml node Lupus81 Visual Basic 2008 Professionals 1 August 4th, 2009 04:56 PM
Displaying data as a Parent Node with Left Node and Right Node Manoj Bisht Visual Basic 2008 Professionals 0 April 2nd, 2009 02:34 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.