eHow launches Android app: Get the best of eHow on the go.

How To

How to Use XML Textwriter

Contributor
By eHow Contributing Writer
(0 Ratings)

XMLTextWriter is a Microsoft .NET class used to write XML files. The class creates a file, writes the XML elements to the file, and then saves it to a folder on the server. This is helpful for website programmers who develop dynamic content for web pages. Using the XMLTextWriter class requires an understanding of C# or Visual Basic syntax.

Difficulty: Moderate
Instructions
  1. Step 1

    Import the XML and IO namespaces to the project. The XMLTextWriter class is not automatically imported into new code files. Use the following syntax at the top of the page:
    using System.IO;
    using System.Xml;

  2. Step 2

    Instantiate the class and create a new file. When instantiating the class, it requires a pointer to an XML file. In this example, the "myfile.xml" file is created:
    XmlTextWriter myXML = new XmlTextWriter("myfile.xml", null);

  3. Step 3

    Write the top, root element to the file. This is the main element that encapsulates all the item lists. This example creates a list of books. The root element used is "books":
    myXML.WriteStartElement("books");

  4. Step 4

    Write the list of books using XML "item" tags. The following writes a list of books under the "books" element created in step 3:
    myXML.WriteElementString("item", "My First Book");
    myXML.WriteElementString("item", "My Second Book");
    myXML.WriteElementString("item", "My Third Book");

  5. Step 5

    Close the root tag element. Just like HTML, XML needs a closing tag. The following code closes the tag:
    myXML.WriteEndElement();

  6. Step 6

    Close the file. Closing the file frees resources on the server and releases it from being locked. If you forget to close the file, the server holds a "read" lock on it. The following syntax closes the file:
    myXML.Close();

References

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics