Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / December 2004

Tip: Looking for answers? Try searching our database.

xsd validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ray@cape.com - 06 Dec 2004 17:46 GMT
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();
Zafar Abbas [MSFT] - 07 Dec 2004 20:09 GMT
You need to change the definition of account_id to be constraint by a
pattern:

<xs:element name="Account_Id" minOccurs="0">
<xs:simpleType>
  <xs:restriction base="xs:string">
   <xs:pattern value="[1-9]*"/>
  </xs:restriction>
 </xs:simpleType>
</xs:element>

> 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.
[quoted text clipped - 73 lines]
> }
> xsd.Close();
ray@cape.com - 07 Dec 2004 21:17 GMT
Thanks for the info. I tried it and wam it worked.
Now is there a way to say that if one element has a value that another
element should have one also. if you look at the phone number elements
there are 3 parts. what i need to do now is say if i have one of the 3
it is required to have all 3? possible or just crazy talk from an xml
wanna be:).
Bruce Wood - 08 Dec 2004 17:39 GMT
I know of no way to specify dependencies like that between independent
elements in XML. However, if you can make the phone number a
sub-element with its own structure, you can specify that all parts must
be there and that the whole phone number element is optional:

<xs:element ... >
<xs:complexType>
<xs:sequence>
<xs:element name="phoneNumber" type="phoneNumberType"
minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="phoneNumber">
<xs:complexType>
<xs:attribute name="areaCode" type="areaCodeType"
use="required" />
<xs:attribute name="exchangeCode" type="exchangeCodeType"
use="requried" />
<xs:attribute name="number" type="phoneNumberNumberType"
use="required" />
   </xs:complexType>
</xs:element
Stan Kitsis [MSFT] - 08 Dec 2004 23:01 GMT
This pattern misses 0's (so a number like 10 won't pass it).  Also, it
doesn't allow for negative numbers.  The follwoing pattern will work better:

<xs:element name="Account_Id" minOccurs="0">
   <xs:simpleType>
       <xs:restriction base="xs:string">
           <xs:pattern value="[+-]?[0-9]*"/>
       </xs:restriction>
   </xs:simpleType>
</xs:element>

However, these values are still strings (they just look like integers) and
as such they allow values like 00009.  You can further modify the pattern to
remove leading zeros (but still allow 0 value):

<xs:element name="Account_Id" minOccurs="0">
   <xs:simpleType>
       <xs:restriction base="xs:string">
           <xs:pattern value="[+-]?([1-9][0-9]*|0"/>
       </xs:restriction>
   </xs:simpleType>
</xs:element>

Signature

Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

> You need to change the definition of account_id to be constraint by a
> pattern:
[quoted text clipped - 84 lines]
>> }
>> xsd.Close();
Stan Kitsis [MSFT] - 08 Dec 2004 23:09 GMT
Sorry, missed the ( ).  Here's what the declarations should look like:

<xs:element name="Account_Id" minOccurs="0">
  <xs:simpleType>
      <xs:restriction base="xs:string">
          <xs:pattern value="([+-]?[0-9]*)?"/>
      </xs:restriction>
  </xs:simpleType>
</xs:element>

and

<xs:element name="Account_Id" minOccurs="0">
  <xs:simpleType>
      <xs:restriction base="xs:string">
          <xs:pattern value="([+-]?([1-9][0-9]*|0)?"/>
      </xs:restriction>
  </xs:simpleType>
</xs:element>

Signature

Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

> This pattern misses 0's (so a number like 10 won't pass it).  Also, it
> doesn't allow for negative numbers.  The follwoing pattern will work
[quoted text clipped - 108 lines]
>>> }
>>> xsd.Close();

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.