XmlTextWriter.LookupPrefix returns the closest prefix in the current namespace scope : XmlTextWriter « XML « C# / C Sharp






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

1.Use XmlTextWriter to create Xml document
2.Write attribute
3.Close an element
4.Control the formatting
5.Write the double value out
6.Closes this stream and the underlying stream.
7.Flush what is in the buffer to the underlying streams and flush the underlying stream.
8.XmlTextWriter.Indentation Property gets or sets how many IndentChars to write
9.XmlTextWriter.WriteCData writes out a block containing the specified text.
10.Use specified Unicode character value.
11.Closes one element and pops the corresponding namespace scope.
12.Writes out the namespace-qualified name.
13.Writes raw markup manually from a string.
14.Writes out the given white space.