Solved

How do I add tags to an xmldocument object?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 …
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 …

911 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

24 Experts available now in Live!

Get 1:1 Help Now