EDN Admin
Well-known member
Greetings Folks,
<p style="margin:0cm 0cm 0pt <span style="font-family:Calibri; font-size:small I am using a nested XSD schema to validate an XML document. The imported XSDs use their own target namespaces and I can validate the sample XML given below using Liquid XML Studio.
But when I run the validation using my C# code below, it fails with the type declaration error (see below). I have spend a lot of time trying to figure out, but no luck:
<span style="font-family:Times New Roman; font-size:small This is the <span style="font-family:"Calibri","sans-serif"; font-size:11pt
Main XSD Schema (DataItem.xsd):
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema <span style="color:Red; xmlnsataNumeric<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; " <span style="color:Red; xmlnsataYesNo<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataYesNo<span style="color:Black; " <span style="color:Red; attributeFormDefault<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; qualified<span style="color:Black; " <span style="color:Red; elementFormDefault<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; qualified<span style="color:Black; " <span style="color:Red; xmlns:xs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/2001/XMLSchema<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; import <span style="color:Red; schemaLocation<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric.xsd<span style="color:Black; " <span style="color:Red; namespace<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; import <span style="color:Red; schemaLocation<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataYesNo.xsd<span style="color:Black; " <span style="color:Red; namespace<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataYesNo<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; tDataItem<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; choice<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric:tDataNumeric<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataYesNo<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataYesNo:tDataYesNo<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; choice<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataItem<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; tDataItem<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema<span style="color:Blue; >
[/code]
<br/>
This is the <span style="font-family:"Calibri","sans-serif"; font-size:11pt Included XSD Schema (DataNumeric.xsd):
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema <span style="color:Red; xmlnsataNumeric<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; " <span style="color:Red; elementFormDefault<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; qualified<span style="color:Black; " <span style="color:Red; targetNamespace<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; " <span style="color:Red; xmlns:xs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/2001/XMLSchema<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; tDataNumeric<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; sequence<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Answer<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:double<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; sequence<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric:tDataNumeric<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema<span style="color:Blue; >
[/code]
This is the actual Xml being validated:
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; DataItem<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; DataNumeric <span style="color:Red; xmlnsataNumeric<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; DataNumeric<span style="color:Blue; :<span style="color:#A31515; Answer<span style="color:Blue; >37.8<span style="color:Blue; </<span style="color:#A31515; DataNumeric<span style="color:Blue; :<span style="color:#A31515; Answer<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; DataNumeric<span style="color:Blue; :<span style="color:#A31515; AmbientTemperature<span style="color:Blue; >28.5<span style="color:Blue; </<span style="color:#A31515; DataNumeric<span style="color:Blue; :<span style="color:#A31515; AmbientTemperature<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; DataNumeric<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; DataItem<span style="color:Blue; >
[/code]
The exception that gets thrown is an <span id="x_nsrTitle XmlSchemaValidationException with the following message:
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt <span><span style="font-family:"Calibri","sans-serif"; font-size:11pt Xml failed schema validation: Type Doc.DataNumeric:tDataNumeric
is not declared.
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt <span style="font-family:"Calibri","sans-serif"; font-size:11pt This is the C# code that performs the validation:
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt
<div style="color:Black;background-color:White; <pre>
XDocument xDoc = XDocument.Parse(XmlDataString);
<span style="color:Blue; string xsdPath = ConfigUtils.GetXsdPath(XsdSchemaIdentifier.HHDataItem);
FileStream fs = <span style="color:Blue; new FileStream(xsdPath, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
XmlSchemaSet xss = <span style="color:Blue; new XmlSchemaSet();
xss.Add(<span style="color:#A31515; "", reader);
fs.Close();
fs.Dispose();
xDoc.Validate(xss, <span style="color:Blue; null);
[/code]
Note: The <span style="font-family:"Calibri","sans-serif"; font-size:11pt ConfigUtils.GetXsdPath(XsdSchemaIdentifier.HHDataItem) is just a call to a utility that returns the full file path to the xsd file.
Any help anyone can give me on this woud be greatly appreciated.
Many thanks,
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt
RobDev
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt
<hr class="sig RobDev
View the full article
<p style="margin:0cm 0cm 0pt <span style="font-family:Calibri; font-size:small I am using a nested XSD schema to validate an XML document. The imported XSDs use their own target namespaces and I can validate the sample XML given below using Liquid XML Studio.
But when I run the validation using my C# code below, it fails with the type declaration error (see below). I have spend a lot of time trying to figure out, but no luck:
<span style="font-family:Times New Roman; font-size:small This is the <span style="font-family:"Calibri","sans-serif"; font-size:11pt
Main XSD Schema (DataItem.xsd):
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema <span style="color:Red; xmlnsataNumeric<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; " <span style="color:Red; xmlnsataYesNo<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataYesNo<span style="color:Black; " <span style="color:Red; attributeFormDefault<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; qualified<span style="color:Black; " <span style="color:Red; elementFormDefault<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; qualified<span style="color:Black; " <span style="color:Red; xmlns:xs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/2001/XMLSchema<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; import <span style="color:Red; schemaLocation<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric.xsd<span style="color:Black; " <span style="color:Red; namespace<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; import <span style="color:Red; schemaLocation<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataYesNo.xsd<span style="color:Black; " <span style="color:Red; namespace<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataYesNo<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; tDataItem<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; choice<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric:tDataNumeric<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataYesNo<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataYesNo:tDataYesNo<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; choice<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataItem<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; tDataItem<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema<span style="color:Blue; >
[/code]
<br/>
This is the <span style="font-family:"Calibri","sans-serif"; font-size:11pt Included XSD Schema (DataNumeric.xsd):
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema <span style="color:Red; xmlnsataNumeric<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; " <span style="color:Red; elementFormDefault<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; qualified<span style="color:Black; " <span style="color:Red; targetNamespace<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; " <span style="color:Red; xmlns:xs<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/2001/XMLSchema<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; tDataNumeric<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; sequence<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Answer<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; xs:double<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; sequence<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; complexType<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; element <span style="color:Red; name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric<span style="color:Black; " <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; DataNumeric:tDataNumeric<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; </<span style="color:#A31515; xs<span style="color:Blue; :<span style="color:#A31515; schema<span style="color:Blue; >
[/code]
This is the actual Xml being validated:
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; DataItem<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; DataNumeric <span style="color:Red; xmlnsataNumeric<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Doc.DataNumeric<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; DataNumeric<span style="color:Blue; :<span style="color:#A31515; Answer<span style="color:Blue; >37.8<span style="color:Blue; </<span style="color:#A31515; DataNumeric<span style="color:Blue; :<span style="color:#A31515; Answer<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; DataNumeric<span style="color:Blue; :<span style="color:#A31515; AmbientTemperature<span style="color:Blue; >28.5<span style="color:Blue; </<span style="color:#A31515; DataNumeric<span style="color:Blue; :<span style="color:#A31515; AmbientTemperature<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; DataNumeric<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; DataItem<span style="color:Blue; >
[/code]
The exception that gets thrown is an <span id="x_nsrTitle XmlSchemaValidationException with the following message:
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt <span><span style="font-family:"Calibri","sans-serif"; font-size:11pt Xml failed schema validation: Type Doc.DataNumeric:tDataNumeric
is not declared.
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt <span style="font-family:"Calibri","sans-serif"; font-size:11pt This is the C# code that performs the validation:
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt
<div style="color:Black;background-color:White; <pre>
XDocument xDoc = XDocument.Parse(XmlDataString);
<span style="color:Blue; string xsdPath = ConfigUtils.GetXsdPath(XsdSchemaIdentifier.HHDataItem);
FileStream fs = <span style="color:Blue; new FileStream(xsdPath, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
XmlSchemaSet xss = <span style="color:Blue; new XmlSchemaSet();
xss.Add(<span style="color:#A31515; "", reader);
fs.Close();
fs.Dispose();
xDoc.Validate(xss, <span style="color:Blue; null);
[/code]
Note: The <span style="font-family:"Calibri","sans-serif"; font-size:11pt ConfigUtils.GetXsdPath(XsdSchemaIdentifier.HHDataItem) is just a call to a utility that returns the full file path to the xsd file.
Any help anyone can give me on this woud be greatly appreciated.
Many thanks,
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt
RobDev
<span style="font-family:Times New Roman; font-size:small <span style="font-family:"Calibri","sans-serif"; font-size:11pt
<hr class="sig RobDev
View the full article