Returns the closest prefix defined in the current namespace scope for the namespace URI. : XmlWriter « XML « C# / C Sharp






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();
        }
    }
}

   
  








Related examples in the same category

1.XmlWriter writes XML data out
2.Closes this stream and the underlying stream.
3.Creates a new XmlWriter instance using the specified stream.
4.Creates a new XmlWriter instance using the stream and XmlWriterSettings object.
5.Creates a new XmlWriter instance using the specified filename.
6.Creates a new XmlWriter instance using the specified TextWriter.
7.Creates a new XmlWriter instance using the TextWriter and XmlWriterSettings objects.
8.Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
9.Writes out all the attributes found at the current position in the XmlReader.
10.Writes an attribute with the specified local name, namespace URI, and value.
11.Encodes the specified binary bytes as Base64 and writes out the resulting text.
12.Encodes the specified binary bytes as BinHex and writes out the resulting text.
13.Writes out a block containing the specified text.
14.Closes one element and pops the corresponding namespace scope.
15.Copies everything from the reader to the writer
16.Copy everything from the XPathNavigator to writer.
17.Write a processing instruction: .
18.Writes text content.
19.Writes a DateTime value.