Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / July 2008

Tip: Looking for answers? Try searching our database.

Add to an XML node

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
wcmcalister@gmail.com - 17 Jul 2008 13:07 GMT
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!
Martin Honnen - 17 Jul 2008 13:31 GMT
> 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<Data> accounts = 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);

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

wcmcalister - 17 Jul 2008 14:06 GMT
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?
Martin Honnen - 17 Jul 2008 14:13 GMT
> 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.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

wcmcalister - 17 Jul 2008 14:54 GMT
Thank you Martin.  I have a lot learn about working with XML in dot Net.

Have a wonderful day (I know I am)!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.