469,364 Members | 2,346 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

Parsing with unqualified Namespace

I am both producing and parsing an xml document that needs to be validated against a schema. I wanted some consumers of the document
to have the option of not performing a validation, so I left the nodes in the instance unqualified. An example of the document is
below:

<?xml version="1.0" encoding="utf-8" ?>
<filingreceipt xmlns="http://www.disclosureusa.org/filingreceipt.xsd">
<cpofilingnumber>TX2004043037501</cpofilingnumber>
<receiptdate>2004-05-01T16:54:21</receiptdate>
<zipfilebytes>95137</zipfilebytes>
<recipientcode>mactx</recipientcode>
</filingreceipt>
When the xmlns attribute is included in the document, I am able to use the XmlValidatingReader to validate the document. However,
parsing the document fails because the parser is looking for the prefix in the unqualified nodes, and I don't have a prefix to pass
to SelectSingleNode. I don't want to qualify the nodes in the document to keep the document simple, and make validating optional. If
I remove the xmlns attribute, XmlValidatingReader refuses to validate the document.

I want to both validate and be able to parse this document, and don't know how to get both the validator and the parser to
cooperate.


Nov 12 '05 #1
4 2237
The XPath expression in SelectSingleNode does not pick up the default
namespace of the xml document.

The following code sample shows how to make use of the namespace manager
that can be passed into SelectSingleNode:
XmlDocument doc = new XmlDocument();
doc.Load("select.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("tns", "http://www.disclosureusa.org/filingreceipt.xsd");

XmlElement root = doc.DocumentElement;
XmlNode receiptdate = root.SelectSingleNode(".//tns:receiptdate", nsmgr);
//You need to qualify the XPath with the namespace of the node that you are
trying to select.
Console.WriteLine(receiptdate.InnerText);

Also, to get no validation using XmlValidatingReader, you can set the
ValidationType property on the reader to ValdiationType.None

Thanks,
Priya

"Erik Moore" <er*******@austin.rr.nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I am both producing and parsing an xml document that needs to be validated against a schema. I wanted some consumers of the document to have the option of not performing a validation, so I left the nodes in the instance unqualified. An example of the document is below:

<?xml version="1.0" encoding="utf-8" ?>
<filingreceipt xmlns="http://www.disclosureusa.org/filingreceipt.xsd">
<cpofilingnumber>TX2004043037501</cpofilingnumber>
<receiptdate>2004-05-01T16:54:21</receiptdate>
<zipfilebytes>95137</zipfilebytes>
<recipientcode>mactx</recipientcode>
</filingreceipt>
When the xmlns attribute is included in the document, I am able to use the XmlValidatingReader to validate the document. However, parsing the document fails because the parser is looking for the prefix in the unqualified nodes, and I don't have a prefix to pass to SelectSingleNode. I don't want to qualify the nodes in the document to keep the document simple, and make validating optional. If I remove the xmlns attribute, XmlValidatingReader refuses to validate the document.
I want to both validate and be able to parse this document, and don't know how to get both the validator and the parser to cooperate.


Nov 12 '05 #2
> The following code sample shows how to make use of the namespace manager
that can be passed into SelectSingleNode:


Thanks-
Is there a way to get the document to validate using the Validating reader without including the namespace declaration in the
instance document?

Nov 12 '05 #3
XmlValidatingReader will look for elements qualified with the
targetNamespace of the schema (if they are glboal elements or local elements
with form=qualified) that the xml is validated against.

If you do not want to declare namespaces in your xml, you should not have a
targetNamespace for the schema.

Thanks,
Priya

"Erik Moore" <er*******@austin.rr.nospam.com> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
The following code sample shows how to make use of the namespace manager
that can be passed into SelectSingleNode:
Thanks-
Is there a way to get the document to validate using the Validating

reader without including the namespace declaration in the instance document?

Nov 12 '05 #4

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 24/05/2004
Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Squid Seven | last post: by
6 posts views Thread by George Ter-Saakov | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.