Mega Code Archive

 
Categories / C# Book / 06 XML
 

0555 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>