Gets or sets a value that specifies how white space is handled.
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main(){
string xmlFrag ="<book> " +
" <title>C#</title>" +
" <genre>Computer</genre>" +
"</book>";
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.Default);
ReadXML(context, xmlFrag, WhitespaceHandling.None);
ReadXML(context, xmlFrag, WhitespaceHandling.All);
}
public static void ReadXML(XmlParserContext context, string xmlFrag, WhitespaceHandling ws){
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
reader.WhitespaceHandling = ws;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Console.WriteLine("{0}: <{1}>", reader.NodeType, reader.Name);
break;
case XmlNodeType.Text:
Console.WriteLine("{0}: {1}", reader.NodeType, reader.Value);
break;
case XmlNodeType.EndElement:
Console.WriteLine("{0}: </{1}>", reader.NodeType, reader.Name);
break;
case XmlNodeType.Whitespace:
Console.WriteLine("{0}:", reader.NodeType);
break;
case XmlNodeType.SignificantWhitespace:
Console.WriteLine("{0}:", reader.NodeType);
break;
}
}
reader.Close();
}
}
Related examples in the same category