472,340 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,340 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 1342

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...
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...
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:...
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...
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...
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...
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 +=...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.