XmlTextWriter.LookupPrefix returns the closest prefix in the current namespace scope
using System;
using System.IO;
using System.Xml;
public class Sample
{
private const string filename = "sampledata.xml";
public static void Main()
{
XmlTextWriter writer = new XmlTextWriter (filename, null);
writer.Formatting = Formatting.Indented;
writer.WriteComment("sample XML fragment");
writer.WriteStartElement("bookstore");
writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");
writer.WriteStartElement("book");
string prefix = writer.LookupPrefix("urn:samples");
writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
writer.WriteString("1-111111-11");
writer.WriteEndAttribute();
writer.WriteStartElement("title");
writer.WriteString("C#");
writer.WriteEndElement();
writer.WriteElementString("price", "19.95");
writer.WriteStartElement(prefix, "style", "urn:samples");
writer.WriteString("hardcover");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
writer.Close();
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(filename);
Console.Write(doc.InnerXml);
}
}
Related examples in the same category