Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 7th, 2009, 09:25 AM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2007
Location: , , .
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.
Reply With Quote
  #2 (permalink)  
Old October 7th, 2009, 09:37 AM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 33%
Activity: 33% Activity: 33% Activity: 33%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 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?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
codehelp (October 12th, 2009)
  #3 (permalink)  
Old October 12th, 2009, 07:41 AM
Authorized User
Points: 135, Level: 2
Points: 135, Level: 2 Points: 135, Level: 2 Points: 135, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2007
Location: , , .
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default Thanks

Thank you Sam.
It works.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 04:30 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 08:46 AM



All times are GMT -4. The time now is 03:21 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.