EDN Admin
Well-known member
We have an issue that we are trying to get past when we are trying to use some code (below) to validate our XML file against an XSD file.
Essentially, validating Xml with Xsd doesnât handle Null values. Using the code below it passes validation if there is a value like <PrinBal>12.45</PrinBal> but fails if the field is Null i.e. <PrinBal />
Anyone have a way to get past this?
The Xsd line for the value is:<br/>
<br/>
<xs:element minOccurs="0" name="PrinBal" type="xs:decimal" />
I realize it is possible to replace the NULL value with something like a 0, which will pass validation. But this wont work for us because we arent able to change the incoming data in any way at all.
Here is the code we are using:
<span style="font-size:10pt; font-family:"Courier New" <span> <span style="color:blue private
<span style="color:#2b91af String ValidateSchema(<span style="color:#2b91af String XsdFile,
<span style="color:#2b91af String XmlFile)
<span style="font-size:10pt; font-family:"Courier New" <span>
{
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:#2b91af XmlReaderSettings Xsdrs = <span style="color:blue
new <span style="color:#2b91af XmlReaderSettings();
<span style="font-size:10pt; font-family:"Courier New" <span>
Xsdrs.Schemas.Add(<span style="color:blue null, XsdFile);
<span style="font-size:10pt; font-family:"Courier New" <span>
Xsdrs.ValidationType = <span style="color:#2b91af ValidationType.Schema;
<span style="font-size:10pt; font-family:"Courier New" <span>
Xsdrs.ValidationEventHandler += <span style="color:blue new <span style="color:#2b91af
ValidationEventHandler(XmlValidationEventHandler);
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:#2b91af XmlReader Xmlr = <span style="color:#2b91af
XmlReader.Create(XmlFile, Xsdrs);
<span style="font-size:10pt; font-family:"Courier New" <span>
ValidMessage = <span style="color:#a31515 "Successful";
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue while (Xmlr.Read()) {}
<span style="font-size:10pt; font-family:"Courier New"
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue return ValidMessage;
<span style="font-size:10pt; font-family:"Courier New" <span>
}
<span style="font-size:10pt; font-family:"Courier New"
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue private <span style="color:blue void XmlValidationEventHandler(<span style="color:blue object sender,
<span style="color:#2b91af ValidationEventArgs args)
<span style="font-size:10pt; font-family:"Courier New" <span>
{
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue if (args.Severity == <span style="color:#2b91af
XmlSeverityType.Warning)
<span style="font-size:10pt; font-family:"Courier New" <span>
{
<span style="font-size:10pt; font-family:"Courier New" <span>
ValidMessage = <span style="color:#a31515 "Warning " + args.Message.ToString();
<span style="font-size:10pt; font-family:"Courier New" <span>
}
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue else <span style="color:blue if (args.Severity ==
<span style="color:#2b91af XmlSeverityType.Error)
<span style="font-size:10pt; font-family:"Courier New" <span>
{
<span style="font-size:10pt; font-family:"Courier New" <span>
ValidMessage = <span style="color:#a31515 "Error: " + args.Message.ToString();
<span style="font-size:10pt; font-family:"Courier New" <span>
}
<span style="font-size:10pt; font-family:"Courier New" <span>
}<span>
View the full article
Essentially, validating Xml with Xsd doesnât handle Null values. Using the code below it passes validation if there is a value like <PrinBal>12.45</PrinBal> but fails if the field is Null i.e. <PrinBal />
Anyone have a way to get past this?
The Xsd line for the value is:<br/>
<br/>
<xs:element minOccurs="0" name="PrinBal" type="xs:decimal" />
I realize it is possible to replace the NULL value with something like a 0, which will pass validation. But this wont work for us because we arent able to change the incoming data in any way at all.
Here is the code we are using:
<span style="font-size:10pt; font-family:"Courier New" <span> <span style="color:blue private
<span style="color:#2b91af String ValidateSchema(<span style="color:#2b91af String XsdFile,
<span style="color:#2b91af String XmlFile)
<span style="font-size:10pt; font-family:"Courier New" <span>
{
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:#2b91af XmlReaderSettings Xsdrs = <span style="color:blue
new <span style="color:#2b91af XmlReaderSettings();
<span style="font-size:10pt; font-family:"Courier New" <span>
Xsdrs.Schemas.Add(<span style="color:blue null, XsdFile);
<span style="font-size:10pt; font-family:"Courier New" <span>
Xsdrs.ValidationType = <span style="color:#2b91af ValidationType.Schema;
<span style="font-size:10pt; font-family:"Courier New" <span>
Xsdrs.ValidationEventHandler += <span style="color:blue new <span style="color:#2b91af
ValidationEventHandler(XmlValidationEventHandler);
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:#2b91af XmlReader Xmlr = <span style="color:#2b91af
XmlReader.Create(XmlFile, Xsdrs);
<span style="font-size:10pt; font-family:"Courier New" <span>
ValidMessage = <span style="color:#a31515 "Successful";
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue while (Xmlr.Read()) {}
<span style="font-size:10pt; font-family:"Courier New"
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue return ValidMessage;
<span style="font-size:10pt; font-family:"Courier New" <span>
}
<span style="font-size:10pt; font-family:"Courier New"
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue private <span style="color:blue void XmlValidationEventHandler(<span style="color:blue object sender,
<span style="color:#2b91af ValidationEventArgs args)
<span style="font-size:10pt; font-family:"Courier New" <span>
{
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue if (args.Severity == <span style="color:#2b91af
XmlSeverityType.Warning)
<span style="font-size:10pt; font-family:"Courier New" <span>
{
<span style="font-size:10pt; font-family:"Courier New" <span>
ValidMessage = <span style="color:#a31515 "Warning " + args.Message.ToString();
<span style="font-size:10pt; font-family:"Courier New" <span>
}
<span style="font-size:10pt; font-family:"Courier New" <span>
<span style="color:blue else <span style="color:blue if (args.Severity ==
<span style="color:#2b91af XmlSeverityType.Error)
<span style="font-size:10pt; font-family:"Courier New" <span>
{
<span style="font-size:10pt; font-family:"Courier New" <span>
ValidMessage = <span style="color:#a31515 "Error: " + args.Message.ToString();
<span style="font-size:10pt; font-family:"Courier New" <span>
}
<span style="font-size:10pt; font-family:"Courier New" <span>
}<span>
View the full article