Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation.
using System;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlTextWriter writer = new XmlTextWriter("out.xml", null);
string tag = "item name";
try
{
writer.WriteStartElement("root");
writer.WriteStartElement(XmlConvert.VerifyName(tag));
}
catch (XmlException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Convert to a valid name...");
writer.WriteStartElement(XmlConvert.EncodeName(tag));
}
writer.WriteString("hammer");
writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();
}
}
Related examples in the same category