Writing Processing Instructions


XML provides the processing instruction as a means to pass information to an application that may parse the document. In the past, applications have abused comments and nonstandard tags to help out applications. For instance, HTML uses the META tag to tell search engines and other robots how the page should be indexed.

Instead of using nonstandard means to communicate with processing applications, you should write a processing instruction into your XML document. Processing instructions start with <? and end with ?> . Directly after the <? is an XML name that specifies the target of the processing instruction. This could be the name of the application that should use the processing instruction, or it could be a unique name specifying the processing instruction. The only restriction on this target is that it must be an XML name . A string follows the target, and its meaning is application-specific.

Processing instructions are markup, but they are not elements, like comments. Processing instructions may appear anywhere in the XML document outside of a tag. This includes the sections before and after the prolog and inside the root element, as well as the sections before and after the epilog. Again, although it is legal for a processing instruction to appear before the prolog, attempting to call WriteProcessingInstruction before WriteStartDocument will raise an InvalidOperationException .

Listing 10.35 shows how to use the WriteProcessingInstruction method to create a common processing instruction, xml-stylesheet. This processing instruction attaches a stylesheet to an XML document.

Listing 10.35
 C# static void Main(string[] args) {   XmlTextWriter writer =     new XmlTextWriter("card.xml", Encoding.UTF8);   writer.Formatting = Formatting.Indented;   writer.WriteProcessingInstruction("xml-stylesheet",     "href=\"card.css\" type=\"text/css\"");   writer.WriteStartElement("BusinessCard");   writer.WriteElementString("Name", "John Doe");   writer.WriteElementString("Phone", "555-555-5555");   writer.WriteElementString("Fax", "999-999-9999");   writer.WriteElementString("E-Mail", "jdoe@mail.com");   writer.WriteEndElement();   writer.Close(); } VB sub Main()   Dim writer As _     New XmlTextWriter("card.xml", Encoding.UTF8)   writer.Formatting = Formatting.Indented   writer.WriteProcessingInstruction("xml-stylesheet", _     "href=" & Chr(34) & "card.css" & Char(34) & _     "type=" & Chr(34) & "text/css" & Chr(34))   writer.WriteStartElement("BusinessCard")   writer.WriteElementString("Name", "John Doe")   writer.WriteElementString("Phone", "555-555-5555")   writer.WriteElementString("Fax", "999-999-9999")   writer.WriteElementString("E-Mail", "jdoe@mail.com")   writer.WriteEndElement()   writer.Close() End Sub 


Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net