I've been trying to solve this issue for a few hours now, the problem is that my validation process doesn't really take into account anything I type inside the restriction.
This is my XSD:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://ift.tt/tphNwY">
<xs:element name="Schools-xml">
<xs:complexType>
<xs:sequence>
<xs:element name="School-xml" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Id"/>
<xs:element name="Preferences">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
Xml Sample:
<?xml version="1.0" encoding="utf-8"?>
<Students_With_Preferences-xml xmlns="http://ift.tt/1GG1t2W">
<Student-xml>
<District>b</District>
<Preferences>(sc1,20)(sc2,15)</Preferences>
</Student-xml>
<Student-xml>
<District>d</District>
<Preferences>(sc2,15)|(sc1,10)</Preferences>
</Student-xml>
As you can see only element with length 5 is allowed, but when I run my code it allows any length. Just to be clear, everything else is validate properly, just this annoying error.
I tried to fix this issue using the regex option but as u guessed its not working as well.
Validation Code(C#):
private Boolean ValidateXML(HttpPostedFileBase file, string xsd_path)
{
bool errors = false;
string filename = "";
XmlSchemaSet schemas = new XmlSchemaSet();
XmlDocument xml_doc = new XmlDocument();
try
{
filename = file.FileName;
schemas.Add("", xsd_path);
xml_doc.Load(file.InputStream);
foreach (XmlNode node in xml_doc.ChildNodes.Item(1).ChildNodes)
{
XDocument doc = XDocument.Parse(node.OuterXml);
doc.Validate(schemas, (o, e) =>
{
ViewBag.msg = "Error validating - " + filename + " - " + e.Message;
errors = true;
});
if (errors) break;
}
}
catch (Exception ex)
{
errors = true;
string msg = "Error validating - " + filename + " - " + ex.Message;
throw new Exception(msg);
}
if (!errors)
{
ViewBag.msg = filename + " was validated succesfully";
}
return !errors;
}
I would really appreciate any help on how to fix this issue, Thanks!
No comments:
Post a Comment