473,549 Members | 2,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trouble validating against xsd

Hello,

I have an xsd file that I'm trying to use to validate some xml against. I'm
hoping this is a simple problem, but I just don't see it. I get the error
"The 'role' element is not declared." when trying to validate this xml:

<users>
<user id="jon" pwd="pass">
<roles><role>Ad min</role></roles>
</user>
</users>

Against this schema:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefa ult="unqualifie d" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="users">
<xs:complexType >
<xs:sequence>
<xs:element name="user" minOccurs="0" maxOccurs="unbo unded">
<xs:complexType >
<xs:sequence>
<xs:element name="roles" minOccurs="0" maxOccurs="1">
<xs:complexType >
<xs:sequence minOccurs="0" maxOccurs="unbo unded">
<xs:element name="role" type="xs:string ">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string " />
<xs:attribute name="pwd" type="xs:string " />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Any thoughts?

Thank you very much!
Jon
Nov 11 '05 #1
4 2397
Jon Poploskie wrote:
I have an xsd file that I'm trying to use to validate some xml against. I'm
hoping this is a simple problem, but I just don't see it. I get the error
"The 'role' element is not declared." when trying to validate this xml:


The schema seems to be ok and xml looks valid against it. In caannot
repreoduce the problim neither in XML Spy nor in .NET. Make sure you
ahve posted real XML and schema and show us your code.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
Hi Jon,

Can you show the code you used to valid the XML file? This may help us
better understand the problem. Thanks.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 11 '05 #3
Oleg,

That was the actual XML fragment and the real schema. Here is the code I'm
using to validate it:

private void ValidateSchema( XmlNode node)
{
Stream xsdFile;

XmlValidatingRe ader val = new XmlValidatingRe ader(new XmlTextReader(
new StringReader(no de.OuterXml)));
val.ValidationT ype = ValidationType. Schema;

xsdFile = Resource.Resour ceManager.GetSt ream("users.xsd ");
val.Schemas.Add (XmlSchema.Read (new XmlTextReader(x sdFile), null));
val.ValidationE ventHandler +=new
ValidationEvent Handler(Validat ionCallBack);

// Read the document
while(val.Read( ) && _isValidDoc) {}
if(!_isValidDoc )
throw new ApplicationExce ption("Not a valid user xml file.");
}

private void ValidationCallB ack( object sender, ValidationEvent Args args )
{
_isValidDoc = false;
}

The code is based on the validation code in the MS UIProcess Application
Block. The only other thing I can think of it that the schema has a
different encoding than the document, would that make a difference (utf-8 vs
uts-16)?

Thanks!
Jon

"Oleg Tkachenko" <oleg@NO_SPAM_P LEASEtkachenko. com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
Jon Poploskie wrote:
I have an xsd file that I'm trying to use to validate some xml against. I'm hoping this is a simple problem, but I just don't see it. I get the error "The 'role' element is not declared." when trying to validate this xml:


The schema seems to be ok and xml looks valid against it. In caannot
repreoduce the problim neither in XML Spy nor in .NET. Make sure you
ahve posted real XML and schema and show us your code.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #4
Jon Poploskie wrote:
That was the actual XML fragment and the real schema. Here is the code I'm
using to validate it:


Well, the only suspicious place left is
Resource.Resour ceManager.GetSt ream("users.xsd ") - are you sure it returns the
schema you have posted?
The following code works ok to me, no validation errors:

public class Test {
static bool _isValidDoc = true;

static void Main(string[] args) {
string xml =
@"<users>
<user id=""jon"" pwd=""pass"">
<roles><role>Ad min</role></roles>
</user>
</users>";
string xsd =
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<xs:schema elementFormDefa ult=""unqualifi ed"" xmlns=""""
xmlns:xs=""http ://www.w3.org/2001/XMLSchema"">
<xs:element name=""users"">
<xs:complexType >
<xs:sequence>
<xs:element name=""user"" minOccurs=""0"" maxOccurs=""unb ounded"">
<xs:complexType >
<xs:sequence>
<xs:element name=""roles"" minOccurs=""0"" maxOccurs=""1"" >
<xs:complexType >
<xs:sequence minOccurs=""0"" maxOccurs=""unb ounded"">
<xs:element name=""role"" type=""xs:strin g"">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name=""id"" type=""xs:strin g"" />
<xs:attribute name=""pwd"" type=""xs:strin g"" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>";
XmlValidatingRe ader val = new XmlValidatingRe ader(new XmlTextReader(
new StringReader(xm l)));
val.ValidationT ype = ValidationType. Schema;

val.Schemas.Add (XmlSchema.Read (new XmlTextReader(n ew
StringReader(xs d)), null));
val.ValidationE ventHandler +=new
ValidationEvent Handler(Test.Va lidationCallBac k);

// Read the document
while(val.Read( ) && _isValidDoc) {}
if(!_isValidDoc )
throw new ApplicationExce ption("Not a valid user xml file.");

}
public static void ValidationCallB ack( object sender, ValidationEvent Args
args )
{
if (args.Severity == XmlSeverityType .Error)
_isValidDoc = false;
}
}

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3609
by: Will | last post by:
I have been having problems validating an XForms document against the XForms schema located at http://www.w3.org/MarkUp/Forms/2002/XForms-Schema.xsd. I have reduced the XForm to its bare bones as follows: <?xml version="1.0" encoding="iso-8859-1"?> <xf:model xmlns:xf="http://www.w3.org/2002/xforms"> <xf:instance xmlns=""/> </xf:model>
1
456
by: davisjoseph | last post by:
Hi All, I'm using Xerces C++ 2.50 DOM version for Validating a valid XML againt the Schema(XSD) available; But it always shows an error in XSD I suppose; This is the error I'm getting using DOMError : Value 'The type of the operation for this entity.' is NOT a valid URI Value ' 1 = Allow 2 = Agree 4 = Deny' is NOT a valid URI; Here '1...
30
3857
by: Toni Mcintyre | last post by:
i'm having 2 problems with the http://validator.w3.org 1. if i have: <meta http-equiv="Content-Script-Type" content="text/javascript"> then why do i need <script type=text/javascript> everywhere??? i'm writing most of my code by hand, so this is a lot of extra typing or global search and replace later 2. the other problem is with my...
9
1380
by: scott | last post by:
I have a style variable below that I can't figure correct quotes for in response.write line. Any help? CODE: sTeamVisible = "visibility: visible;" Response.Write "<td class=""teamdrop1"" style=""" & sTeamVisible & """ & " "
1
4311
by: Christian | last post by:
Hi, I load an Xml-file "customers.xml" into a DataSet (works fine) but then how do I validate it against a schema (e.g. customers.xsd) ? my customers.xml: <?xml version="1.0" encoding="utf-8"?>| <customers xmlns="http://tempuri.org/customers.xsd"> <Customer ID="1000"> <FirstName>Greg</FirstName>
0
1058
by: Shadow | last post by:
Hello, I am using the XmlValidatingReader to validate xml against an XSD schema. Some of the elements in the XSD have a max length set. I am having issues validating fields containing xml 'special' characters. The validator barks that I have exceeded the max length when in fact the actual text is below the max length. I have determined...
2
7148
by: josh | last post by:
Hi, I am trying to validate cXML documents against cXML.dtd using the XmlValidatingReader. If I set the XMLValidatingReader's ValidatingType to ValidationType.DTD, I get the following System.Xml.Schema.XmlSchemaException: "The parameter entity replacement text must nest properly within markup
0
987
by: MarkJohnson | last post by:
Hi, I have a question about validating an XML document against an XSD schema. If I have an element with name "XYZ" and of complex type "ABC", can I validate against either the element name, type or both? e.g. XSD: <xsd:element name="XYZ" type="ism:ABC"/> <xsd:complexType name="ABC"> <ABC content>
7
4218
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
I wrote a method to validate and xml file against a schema. If the file does not conform to the schema, it throws an error. It works fine except for one curious thing. If I try to validate an XDocument (containing schema xml) against a schema url, it validates successfully..... Has anyone ever seen this before or know why it does this. ...
0
7457
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7817
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.