?
Solved

How do I add tags to an xmldocument object?

Posted on 2011-03-21
2
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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 …

650 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