I'm new to XML and XSD and struggling with what I believe should be a fairly simple task.
I have an xml file (GTR_000120040106113134888.XML) with the following contents, note the xml version is missing as this is how the file is supplied by the third party:
<Vehicles>
<Vehicle>
<IsChanged>0</IsChanged>
</Vehicle>
</Vehicles>
I also have an XSD file (GTR.xsd) with the following contents:
<?xml version="1.0"?>
<xs:schema id="Vehicles" xmlns:xs=" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="Vehicles">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Vehicle">
<xs:complexType>
<xs:sequence>
<xs:element name="IsChanged" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
I'm trying to validate the XML against the XSD using the following C# method:
private bool ValidateXMLAgainstSchema(string XMLFile, string XSDFile)
{
try
{
misValid = true;
XmlTextReader xml = new XmlTextReader(XMLFile);
XmlSchema schema = new XmlSchema();
XmlValidatingReader xsd = new XmlValidatingReader(xml);
xsd.Schemas.Add("", XSDFile);
xsd.ValidationType = ValidationType.Schema;
xsd.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);
while (xsd.Read())
{
}
xsd.Close();
}
catch (Exception ex)
{
string a = ex.Message;
misValid = false;
}
return misValid;
}
When the code reaches xsd.Schemas.Add("", XSDFile);, I get the following error:
The attribute targetNamespace does not match the designated namespace URI. An error occurred at file:///C:/$Crypt/DataFiles/StockImport/Uploaded/XSD/GTR.xsd, (2, 2).
Can somebody please explain what this error means as I have no idea.
Many Thanks
I have an xml file (GTR_000120040106113134888.XML) with the following contents, note the xml version is missing as this is how the file is supplied by the third party:
<Vehicles>
<Vehicle>
<IsChanged>0</IsChanged>
</Vehicle>
</Vehicles>
I also have an XSD file (GTR.xsd) with the following contents:
<?xml version="1.0"?>
<xs:schema id="Vehicles" xmlns:xs=" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="Vehicles">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Vehicle">
<xs:complexType>
<xs:sequence>
<xs:element name="IsChanged" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
I'm trying to validate the XML against the XSD using the following C# method:
private bool ValidateXMLAgainstSchema(string XMLFile, string XSDFile)
{
try
{
misValid = true;
XmlTextReader xml = new XmlTextReader(XMLFile);
XmlSchema schema = new XmlSchema();
XmlValidatingReader xsd = new XmlValidatingReader(xml);
xsd.Schemas.Add("", XSDFile);
xsd.ValidationType = ValidationType.Schema;
xsd.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);
while (xsd.Read())
{
}
xsd.Close();
}
catch (Exception ex)
{
string a = ex.Message;
misValid = false;
}
return misValid;
}
When the code reaches xsd.Schemas.Add("", XSDFile);, I get the following error:
The attribute targetNamespace does not match the designated namespace URI. An error occurred at file:///C:/$Crypt/DataFiles/StockImport/Uploaded/XSD/GTR.xsd, (2, 2).
Can somebody please explain what this error means as I have no idea.
Many Thanks