Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybodyA

I want to predefine an XML schema for a DataTable object. This alone is no problem.
But how can i force the ReadXML method (or similar) to stick to this schema, instead of just replacing it by the XML file's schema? When the schema in the file is different there should be an exception or just no loading of data.
How would that code look like exactly, if it works in the end? Do all contraints and elements of the XML file and the DataTable have to be the same?

Thank you very much in advance!

sample file:
<?xml version="1.0" standalone="yes"?>
<Parameter_x0020_File>
 <xs:schema id="Parameter_x0020_Files" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Parameter_x0020_Files" msdata:IsDataSet="true" msdata:MainDataTable="Tools" msdata:UseCurrentLocale="true">
   <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
     <xs:element name="Tools">
      <xs:complexType>
       <xs:sequence>
        <xs:element name="ID" msdata:ReadOnly="true" type="xs:string" />
         <xs:element name="Name" type="xs:string" minOccurs="0" />
       </xs:sequence>
      </xs:complexType>
     </xs:element>
    </xs:choice>
   </xs:complexType>
  </xs:element>
 </xs:schema>
 <Tools>
  <ID>10</ID>
  <Name>sample</Name>
 </Tools>
</Parameter_x0020_File>
Posted

1 solution

Wouldn't it be easier if you validated your XML with XSD before loading and throw an error if it fails ?

I might be missing something here..please let me know if that approach works

C#
XmlSchemaSet mySch= new XmlSchemaSet();
mySch.Add(SCH_NAMESPACE, YOUR_SCHEMA_FILE);

XDocument doc = XDocument.Load(YOU_XML_FILENAME);
string errMsg = "";
doc.Validate(mySch, (o, e) => {
    msg += e.Message + Environment.NewLine;
});
if(msg != "")
{
//handle error here
}
else
{
//load datatable
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900