Solved

How do I add tags to an xmldocument object?

Posted on 2011-03-21
2
246 Views
Last Modified: 2012-05-11
I'm trying to do this in C#. Here is a sample...

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("MyXml.xml");
            //while(pages) {add page tags}
            xmldoc.Save("MyXml.xml");



From this....

<documents>
      <pages>
      </pages>
</documents>


To this....

<documents>
      <pages>
        <page number="0">page0.TIF</page>
        <page number="1">page1.TIF</page>
      </pages>
</documents>
0
Comment
Question by:silentthread2k
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35181229
Very simple example:
            XmlDocument doc = new XmlDocument();
            doc.Load("MyXml.xml");

            XmlNode root = doc.SelectSingleNode("//pages");

            for (int i = 0; i != 10; ++i)
            {
                XmlNode child = doc.CreateElement("page");

                XmlAttribute attr = doc.CreateAttribute("number");
                attr.Value = i.ToString();
                child.Attributes.Append(attr);

                child.InnerText = string.Format("page{0}.TIF", i);
                root.AppendChild(child);
            }

            doc.Save("MyXml.xml");

Open in new window

0
 

Author Closing Comment

by:silentthread2k
ID: 35183793
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now