Solved

How do I add tags to an xmldocument object?

Posted on 2011-03-21
2
249 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sent email using Shared Mailbox from outlook in C# 5 50
ASP/VB email question 4 46
incorrect syntax near the order by 10 42
Name Space error VS2015 1 25
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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