Jack White said:
Since none of the examples I've seen turn this flag on it leads me to
believe I have a problem somewhere. I shouldn't have to turn it on IOW if
nobody else has to.
It always reports it as a warning. I originally thought it would be
reported as an error but apparently not. The problem is that you can't
distinguish between "acceptable" warnings generated while reading a
conforming ".xml" file (warnings I can safely ignore), and those that
really need to be treated as errors (normally because you're dealing with
a non-conforming ".xml" file). My testing shows that a conforming ".xml"
file generates no warnings however so I'll have to assume that any warning
is really an error and treat it that way. That may not be true however so
I may actually reject a conforming ".xml" file which will be a problem. I
can't seem to resolve the issue any other way however.
Even when I pass "XmlWriteMode.WriteSchema" to "WriteXml()" and later read
it back in, no errrors are generated.
Tampering isn't an issue in my case but it's really an implemenation
detail so I wanted to avoid having to install it merely for this purpose.
I'm not sure what the accepted protocol is however . To validate an ".xml"
file, do you normally install its ".xsd" file for this purpose, assuming
you don't need it for anything else. In any case, my overall experience
with this situation has been very frustrating. I just want to validate an
".xml" file but apparently I have to become an XML expert to do it. Thanks
again for your help though.
Don't worry about distributing an XSD with your application, it's just
another file. And it's there for the purpose of making your application
inputs more robust.
Anyway, back your the validation problem...
Before calling the myDataSet.ReadXml( reader ), you need to call while (
reader.Read() ); Doing so will cause the reader to walk over the document
and pick up any problems.
I promise you, this version works, honest ;-)
class Program
{
static void Main()
{
try
{
//WriteData();
//Mangle the data by hand...
ReadData();
}
catch ( Exception exception )
{
Console.WriteLine( exception.Message );
}
finally
{
Console.WriteLine( "\r\nFinished" );
Console.ReadLine();
}
}
static void WriteData()
{
MyDataSet testDataSet = new MyDataSet();
testDataSet.MyTable.AddMyTableRow( "Abraham Lincoln" );
testDataSet.WriteXml( "MyFile.xml", XmlWriteMode.IgnoreSchema );
}
/// <summary>
///
/// </summary>
static void ReadData()
{
MyDataSet myDataSet = new MyDataSet();
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add( null, "..\\..\\MyDataSet.xsd" );
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |=
XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(
delegate( object sender, ValidationEventArgs args )
{
Console.WriteLine( "Severity : {0}\r\nMessage :{1}",
args.Severity.ToString(), args.Message );
} );
XmlReader reader = XmlReader.Create( "MyFile.xml", settings );
//this line is important!!!!
while ( reader.Read() ) ;
myDataSet.ReadXml( reader );
}
}