I have been working on this for a few days now and can't seem to figure
out how to do it or if it is even possible.
I have and excel spreadsheet that when the user clicks a button it
creates the xml and then the user uploads it to a server. What i want
to do is have the xsd file that is referenced in the xml doc validate
the data ie. if it is required, if it is of the right type ect.
Now the xml file may or may not have a value for a field(account_id).
if it has a value i want it to be only of integer. If it has no value
thats ok to.
How do i do this? I have tried several sugestions that i have found on
this board but just can't seem to get it right.
Thanks for anyhelp you can give and thanks for all the help that you
already have.
Ray
My xml file:
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="LeadXSD.xsd">
- <lead>
<Company>ASTI</Company>
<Account_Id />
<Account_Contact_Id >88</Account_Contact_Id >
</lead>
</data>
My xsd file: it is the account_id field that i am interested in.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="data">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="lead">
<xs:complexType>
<xs:sequence>
<xs:element name="Company" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="64" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Account_Contact_Id" minOccurs="0">
<xs:simpleType>
<xs:restriction base xs:string"></xs:restriction>
</xs:simpleType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
My partial C# code:
string xmlDocument = xmlFile
XmlTextReader xml = new XmlTextReader(xmlDocument);
XmlValidatingReader xsd = new XmlValidatingReader(xml);
xsd.ValidationType = ValidationType.Schema;
//and validation errors events go to...
xsd.ValidationEventHandler += new
ValidationEventHandler(MyValidationEventHandler);
while (xsd.Read())
{
}
xsd.Close();