473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Problem with partial validation

I have a problem when I select node elements from an xml file and validata
each node againts the schema. I use XmlValidatingReader and it complains
about elements not being declared.

I have defined a schema for details of a particular service request. Below
is a schema similar to the one that I defined:

<?xml version="1.0" standalone="yes" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.mycompany.com"
xmlns="http://www.mycompany.com" xmlns:dsc="http://www.mycompany.com"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"
elementFormDefault="qualified">
<xs:element name="Requests">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Request">
<xs:complexType>
<xs:sequence>
<xs:element name="Input1" type="xs:unsignedLong" minOccurs="1"
maxOccurs="1" />
<xs:element name="Input2" type="xs:unsignedLong" minOccurs="1"
maxOccurs="1" />
<xs:element name="Response" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Result" type="xs:boolean" />
<xs:element name="Message" type="xs:string" />
<xs:element name="DateTime" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

A simple Xml file that conforms to the above schema would have batched
requests and look like

<?xml version="1.0"?>
<tst:Requests xmlns:tst="http://www.mycompany.com">
<tst:Request>
<tst:Input1>1</tst:Input1>
<tst:Input2>2</tst:Input2>
</tst:Request>
<tst:Request>
<tst:Input1>3</tst:Input1>
<tst:Input2>4</tst:Input2>
</tst:Request>
</tst:Requests>

I have a program that validates and processes this XML file. What I would
like to do is that if the xml file is valid but some of the request elements
do not conform to my schema, I would still process the ones that are valid
(against my schema). So what I do is:

1) Load the Xml file and select the request nodes. This step would make sure
that the xml file is well formed (valid). Below is the snippet and it works
fine.

XmlTextReader rdr = new XmlTextReader(xmlFile);
System.Xml.XmlDocument doc = new XmlDocument();
doc.Load(rdr);
//Create a namespace manager
System.Xml.XmlNamespaceManager nsMngr = new
XmlNamespaceManager(doc.NameTable);
nsMngr.AddNamespace("tst","http://www.mycompany.com");
//
System.Xml.XmlNodeList rqstLst =
doc.SelectNodes("tst:Requests/tst:Request",nsMngr);
//
foreach(XmlNode nd in rqstLst)
{
if( ValidateRequestXml(nd.OuterXml))
{
ProcessRequest(nd.OuterXml);
}
}

2) In ValidateRequestXml, I try to validate just the part of the Xml file
(request element). If it is ok, I can process it. Otherwise, I go to the
next request. This way, a request that does not conform to my schema will
not prevent the processing of valid requests. This is what I do:
NameTable nt = new NameTable();
XmlNamespaceManager nsMngr = new XmlNamespaceManager(nt);
nsMngr.AddNamespace("txt","http://www.mycompany.com");
System.Xml.XmlParserContext rqstCtx = new
XmlParserContext(null,nsMngr,null,XmlSpace.None);
XmlValidatingReader vRdr = new
XmlValidatingReader(xmlRequest,XmlNodeType.Element ,rqstCtx); //xmlRequest is
nd.OuterXml from step 1
vRdr.Schemas.Add("http://www.mycompany.com",xmlSchema);
vRdr.ValidationEventHandler +=new
ValidationEventHandler(vRdr_ValidationEventHandler );
Requests ds = new Requests(); //Dataset generated from the schema. I
just read each request into it
ds.ReadXml(vRdr);

xmlRequest is the OuterXml of the selected node elements passed to
ValidateRequestXml and includes a namespace definition. For examples, it
would look like:

<tst:Request xmlns:tst="http://www.mycompany.com">
<tst:Input1>1</tst:Input1>
<tst:Input2>2</tst:Input2>
</tst:Request>

However, the validating reader seems to have a problem with the inclusion of
the definition of the namespace in the node element. It raises validation
events when I call ds.ReadXml(vRdr) saying that

'http://www.mycompany.com:Request" element not is not declared.
'http://www.mycompany.com:Input1" element not is not declared.
'http://www.mycompany.com:Input2" element not is not declared.

Is the definition of namespace in the selected node causing the validation
to look for the declaration of the elements in a subnamespace?

If I remove the namespace definition from the selected node, the validation
seems to be working ok. Could someone give me an explaination and possibly a
solution or an alternative way of doing partial (element-only) schema
validations?

Your responses and time are deeply appreciated.

Matt
Jan 11 '06 #1
0 1391

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

Similar topics

7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
9
by: brandon | last post by:
I want to speed up the validation of some of my fairly large and detailed XML files. In particular there is a set of child elements and attributes that does not to be validated after it has...
2
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be...
3
by: john morales | last post by:
Hi guys, I have a problem and i know there must be a solution for this as it is such a basic common practice in asp.net development. Scenario: i have many webforms in a site, most with two...
0
by: js | last post by:
My form has only one RequiredFieldValidator (called RqValidateSerial) enabled at design time. In the code behind I set the validatior to false when the form is submitted by certain button click. ...
2
by: B. Chernick | last post by:
I have an odd problem with validation and I'm not sure if the problem's in ASP, VB, Javascript, or a third-party component. We created a user control (ASP1.1) that encapsulates a third party...
0
by: bbalet.free.fr | last post by:
The “Add Web Reference” Visual tool generates bad classes (from WSDL schema) for ComplexType containing only one element (wsdl.exe and wseWsdl3.exe tools have the same problem) : if a...
0
by: Microsoft News Group | last post by:
I am adding some field validation throught column changing with partial classes. I am debugging it and when it hits this.ColumnChanging += ValidateColumn; It never goes to the method void...
2
by: swethak | last post by:
hi , i write the code in .htm file. It is in cgi-bin/searches/one.htm.In that i write a form submitting and validations.But validations are not worked in that .htm file. I used the same code in my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.