Just load it and catch the exception. Same for File.Exists()- the file system is volatile so just because File.Exists() returns true doesn't mean you'll be able to open it.
views:
1011answers:
6There are plenty of examples of how to validate xml against a schema on the interwebs.
It's probably just worth catching the specific exception if you want to show a message to the user:
try
{
XDocument xd1 = new XDocument();
xd1 = XDocument.Load(myfile);
}
catch (XmlException exception)
{
ShowMessage("Your XML was probably bad...");
}
If you have an XSD for the XML, try this:
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class ValidXSD {
public static void Main() {
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);
// Parse the file.
while (reader.Read());
}
// Display any warnings or errors. private static void ValidationCallBack (object sender, ValidationEventArgs args) { if (args.Severity==XmlSeverityType.Warning) Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message); else Console.WriteLine("\tValidation error: " + args.Message);
}
}
Reference is here:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreadersettings.validationeventhandler.aspx
This question confuses "well-formed" with "valid" XML document.
A valid xml document is by definition a well formed document. Additionally, it must satisfy a DTD or a schema (an xml schema, a relaxng schema, schematron or other constraints) to be valid.
Judging from the wording of the question, most probably it asks:
"How to make sure a file contains a well-formed XML document?".
The answer is that an XML document is well-formed if it can be parsed successfully by a compliant XML parser. As the XDocument.Load() method does exactly this, you only need to catch the exception and then conclude that the text contained in the file is not well formed.
As has previously been mentioned "valid xml" is tested by XmlDocument.Load(). Just catch the exception. If you need further validation to test that it's valid against a schema, then this does what you're after:
using System.Xml;
using System.Xml.Schema;
using System.IO;
static class Program
{
private static bool _Valid = true; //Until we find otherwise
private static void Invalidated()
{
_Valid = false;
}
private static bool Validated(XmlTextReader Xml, XmlTextReader Xsd)
{
var MySchema = XmlSchema.Read(Xsd, new ValidationEventHandler(Invalidated));
var MySettings = new XmlReaderSettings();
{
MySettings.IgnoreComments = true;
MySettings.IgnoreProcessingInstructions = true;
MySettings.IgnoreWhitespace = true;
}
var MyXml = XmlReader.Create(Xml, MySettings);
while (MyXml.Read) {
//Parsing...
}
return _Valid;
}
public static void Main()
{
var XsdPath = "C:\\Path\\To\\MySchemaDocument.xsd";
var XmlPath = "C:\\Path\\To\\MyXmlDocument.xml";
var XsdDoc = new XmlTextReader(XsdPath);
var XmlDoc = new XmlTextReader(XmlPath);
var WellFormed = true;
XmlDocument xDoc = new XmlDocument();
try {
xDoc.Load(XmlDoc);
}
catch (XmlException Ex) {
WellFormed = false;
}
if (WellFormed & Validated(XmlDoc, XsdDoc)) {
//Do stuff with my well formed and validated XmlDocument instance...
}
}
}