Hello everyone,
I am checking againtst whether a specific input string are valid (could be used as the value for an XML element) in XML UTF-8 encoding. My goal is to tell which string (from an input string array) is not valid according to XML UTF-8 encoding standard.
Here is my code, my current implementation is straightforward -- assemble XML file with each individual string from the input string array. I am not sure whether it is the most efficient way. From functional point of view, it works.
My working environment is .Net 3.5 + VSTS 2008 + C#.
static void Main(string[] args)
{
string[] inputs = { "Hello", "World", "StackOverflow", "ServerFault", "&#DFFE" };
XmlDocument xDoc = new XmlDocument();
string header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
string formatter = "<foo>{0}</foo>";
foreach (string item in inputs)
{
StringBuilder builder = new StringBuilder();
builder.Append (header);
builder.Append (String.Format(formatter, item));
try
{
xDoc.Load(new StringReader(builder.ToString()));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
thanks in advance, George