Solved

How do I add tags to an xmldocument object?

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
A short article about problems I had with the new location API and permissions in Marshmallow
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

803 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