Mega Code Archive

 
Categories / C# / XML
 

Returns the closest prefix defined in the current namespace scope for the namespace URI

using System; using System.IO; using System.Xml; public class Sample {     private const string m_Document = "sampledata.xml";     public static void Main()     {         XmlWriter writer = null;         try         {             XmlWriterSettings settings = new XmlWriterSettings();             settings.Indent = true;             writer = XmlWriter.Create(m_Document, settings);             writer.WriteComment("sample XML fragment");             writer.WriteStartElement("book");             writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");             writer.WriteAttributeString("genre", "Computer");             writer.WriteStartElement("title");             writer.WriteString("C#");             writer.WriteEndElement();             writer.WriteElementString("price", "1.95");             string prefix = writer.LookupPrefix("urn:samples");             writer.WriteStartElement(prefix, "ISBN", "urn:samples");             writer.WriteString("1-111111-78");             writer.WriteEndElement();             writer.WriteElementString("style", "urn:samples", "hardcover");             writer.WriteEndElement();             writer.Flush();             writer.Close();         }         finally         {             if (writer != null)                 writer.Close();         }     } }