Verifies that the name is a valid name according to the W3C Extended Markup Language recommendation. : XML Convert « XML LINQ « C# / C Sharp






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

1.Decodes a name. This method does the reverse of the EncodeName and EncodeLocalName methods.
2.Converts the supplied String to a DateTimeOffset equivalent.
3.Converts the DateTime to a String.
4.Converts the supplied DateTimeOffset to a String.
5.Converts the supplied DateTimeOffset to a String in the specified format.
6.Verify Xml Chars