What is the fastest way to programmatically check the well-formedness of XML files in C#?
I would expect that XmlReader
with while(reader.Read)() {}
would be the fastest managed approach. It certainly shouldn't take seconds to read 40KB... what is the input approach you are using?
Do you perhaps have some external (schema etc) entities to resolve? If so, you might be able to write a custom XmlResolver
(set via XmlReaderSettings
) that uses locally cached schemas rather than a remote fetch...
The following does ~300KB virtually instantly:
using(MemoryStream ms = new MemoryStream()) {
XmlWriterSettings settings = new XmlWriterSettings();
settings.CloseOutput = false;
using (XmlWriter writer = XmlWriter.Create(ms, settings))
{
writer.WriteStartElement("xml");
for (int i = 0; i < 15000; i++)
{
writer.WriteElementString("value", i.ToString());
}
writer.WriteEndElement();
}
Console.WriteLine(ms.Length + " bytes");
ms.Position = 0;
int nodes = 0;
Stopwatch watch = Stopwatch.StartNew();
using (XmlReader reader = XmlReader.Create(ms))
{
while (reader.Read()) { nodes++; }
}
watch.Stop();
Console.WriteLine("{0} nodes in {1}ms", nodes,
watch.ElapsedMilliseconds);
}
Create an XmlReader
object by passing in an XmlReaderSettings
object that has the ConformanceLevel.Document
.
This will validate well-formedness.
This MSDN article should explain the details.