Create XmlTextReader with the specified string, XmlNodeType, and XmlParserContext.
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
string xmlFrag ="<book> " +
"<title>C#</title>" +
"<bk:genre>Programming</bk:genre>" +
"</book>";
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("bk", "urn:sample");
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
while (reader.Read()){
if (reader.IsStartElement()){
if (reader.Prefix==String.Empty)
Console.WriteLine("<{0}>", reader.LocalName);
else{
Console.Write("<{0}:{1}>", reader.Prefix, reader.LocalName);
Console.WriteLine(" The namespace URI is " + reader.NamespaceURI);
}
}
}
reader.Close();
}
}
Related examples in the same category