Groups | Blog | Home
all groups > dotnet xml > may 2004 >

dotnet xml : Parsing with unqualified Namespace



Priya Lakshminarayanan [MSFT]
5/21/2004 1:32:54 PM
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

[quoted text, click to view]

Erik Moore
5/21/2004 2:36:29 PM
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.





Erik Moore
5/24/2004 9:59:52 AM
[quoted text, click to view]

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


Priya Lakshminarayanan [MSFT]
5/24/2004 2:25:55 PM
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

[quoted text, click to view]

Daniel Cazzulino [MVP XML]
5/26/2004 11:05:57 AM



---
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

AddThis Social Bookmark Button