does my stream contain valid XML?

  • Thread starter Thread starter LJB
  • Start date Start date
L

LJB

If I read a text file from disk into a stream how can I tell if it might
already be valid XML? I'm writing an app to compare two XML files one of
which may need conversion to XML first. My app will do the conversion when
necessary. I plan to call XMLdiffView, from
http://msdn2.microsoft.com/en-us/library/aa302295.aspx, to generate the
differences report.

thanks
LJB
 
If I read a text file from disk into a stream how can I tell if it might
already be valid XML? I'm writing an app to compare two XML files one of
which may need conversion to XML first. My app will do the conversion when
necessary. I plan to call XMLdiffView, from
http://msdn2.microsoft.com/en-us/library/aa302295.aspx, to generate the
differences report.

The easiest way would be to just load the stream as an XML document. If
you can load it without an exception occurring, it's valid XML. I know
it's not exactly elegant, but it's simple...
 
Perhaps attach an XmlReader, MoveToContent() and Skip() the whole
thing? IIRC this will still throw an exception if it is invalid...
(but you'd need to verify by testing).

If your stream is seekable this is a pinch; if not, and your stream is
repeatable then simply open it twice in a row. My post just bombed and
I can now see Jon's reply; XmlDocument will also work, but could
perhaps be a hog for large xml. It would be perfectly adequate for
small-to-moderate size though.

Marc
 
If I read a text file from disk into a stream how can I tell if it might
already be valid XML? I'm writing an app to compare two XML files one of
which may need conversion to XML first. My app will do the conversion when
necessary. I plan to call XMLdiffView, from
http://msdn2.microsoft.com/en-us/library/aa302295.aspx, to generate the
differences report.

See members "ValidationEventHandler", "ValidationType" and "ValidationFlags" in class "XmlReaderSettings". Here's a cut-and-paste example:

public void LoadFromFile(string fileName)
{
// ...

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, GetSchemaFullFileName()); // ".xsd" file
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

///////////////////////////////////////////////////////////////
// Assign an anonymous delegate to trap any schema violations
// when "ReadXml()" is called further below. That is, the
// delegate will be invoked if "fileName" doesn't conform to
// the schema found in the ".xsd" file plugged in above. Note
// that the delegate simply throws an exception on the first
// problem encountered.
///////////////////////////////////////////////////////////////
settings.ValidationEventHandler +=
delegate(object sender, ValidationEventArgs args)
{
throw new InvalidFileException(fileName, args.Exception);
};

// Create a reader for "fileName" using the above settings
using (XmlReader reader = XmlReader.Create(fileName, settings))
{
try
{
////////////////////////////////////////////////////////////
// Read the file into the calling object (us). Note that
// the delegate above will be called if the file doesn't
// conform to the schema file we plugged in earlier. Other
// errors are also possible however but these will normally
// be of type "XmlException" so we specifically catch that
// below and re-throw it as our own custom exception. Note
// that such errors are usually XML syntax errors however
// so they won't normally be encountered unless someone
// has mucked with the file or otherwise passed some other
// invalid XML file.
////////////////////////////////////////////////////////////
ReadXml(reader);
}
catch (XmlException exception)
{
throw new InvalidFileException(fileName, exception);
}
finally
{
reader.Close();
}
}

// ...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top