Namespaces and Prefixes

The overloads for the Write* methods allow you to associate an element or attribute with a namespace.


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
class Program
{
    static void Main()
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using (XmlWriter writer = XmlWriter.Create("foo.xml", settings))
        {

            writer.WriteStartElement("o", "customer", "http://yourDomain");
            writer.WriteElementString("o", "firstname", "http://yourDomain", "Jack");
            writer.WriteElementString("o", "lastname", "http://yourDomain", "Smith");
            writer.WriteEndElement();
        }
    }
}

The output:

 
        <?xml version="1.0" encoding="utf-8"?>
        <o:customer xmlns:o="http://yourDomain">
          <o:firstname>Jack</o:firstname>
          <o:lastname>Smith</o:lastname>
        </o:customer>
  
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.