dotnet xml:
I have a problem with the
XmlTextWriter.WriteProcessingInstruction method. This
method takes two parameters:
[C#]
public override void WriteProcessingInstruction(
string name,
string text
);
Remarks
If text is either a null reference (Nothing in Visual
Basic) or String.Empty, this method writes a
ProcessingInstruction with no data content, for example <?
name?>.
So i tried to write a processing instruction with the
parameter text set to null. What i got wasn't what i
expected: <?name ?>. This format is not well formed.
Is this a bug or do someone have an advice for correcting
the problem?
Here a small testprogramm:
using System;
using System.IO;
using System.Xml;
public class Sample
{
private const string filename = "testPI.xml";
public static void Main()
{
XmlTextWriter writer = null;
writer = new XmlTextWriter (filename,
null);
//Use indenting for readability.
writer.Formatting = Formatting.Indented;
//Write the XML delcaration.
writer.WriteStartDocument();
//Write the ProcessingInstruction node.
writer.WriteProcessingInstruction("name",
null);
//Write a root element.
writer.WriteStartElement("book");
//Write the genre attribute.
writer.WriteAttributeString
("genre", "novel");
//Write the close tag for the root element.
writer.WriteEndElement();
//Write the XML to file and close the
writer.
writer.Flush();
writer.Close();
//Load the file into an XmlDocument to
ensure well formed XML.
XmlDocument doc = new XmlDocument();
//Preserve white space for readability.
doc.PreserveWhitespace = true;
//Load the file.
doc.Load(filename);
//Display the XML content to the console.
Console.Write(doc.InnerXml);
Console.ReadLine();
}
}