Using an XML Namespace Prefix with DataSets and XmlDataDocument

K

Kristin

I'd like to use both the XmlDataDocument and an ADO strongly-typed dataset to access my application's XML data store.

However, I can't seem to get the two access mechanisms to be compatible with my namespace prefix declaration. At this point, if the XML input file contains a namespace prefix, my XPath query through the XmlDataDocument works great, but the corresponding dataset is not loaded and the dataset query fails. (I've renamed identifyers to keep things simple).
<MyData xmlns:md="http://www.mydomain.com/MyDataSet.xsd">

Conversely, if the XML input file has no namespace prefix, the dataset query suceeds but the XPath query of the XmlDataDocument fails.

<MyData xmlns="http://www.mydomain.com/MyDataSet.xsd">

I've attempted to give both the XmlDataDocument and the generated dataset code the context to deal with prefix or lack thereof. But I can't get them both working at the same time.

Can anyone shed some light on how to get one of these (ideally the one including the prefix) working?

Thanks!!
-Kristin

Here's the code:
public void Open (string fileName)
{
// Create the typed dataset and associate it with an XML document
Data.MyDataSet ds = new Data.MyDataSet();
XmlDataDocument xmlDoc = new XmlDataDocument (ds);

// Load the XML
xmlDoc.Load (fileName);

string result = null;
try
{

// Use the dataset to query the name of the first staff member

// If the namespace prefix is **present**,
// <MyData xmlns:md="http://www.mydomain.com/MyDataSet.xsd">
// the dataset is empty and this call fails. Otherwise, it suceeds.
result = ds.StaffMember[0].FirstName;
}
catch (Exception e)
{
result = "**failed**\n" + e.Message;
}
MessageBox.Show ("Dataset query: " + result);

try
{

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace ("md", "http://www.mydomain.com/MyDataSet.xsd");
nsMgr.AddNamespace (String.Empty, "http://www.mydomain.com/MyDataSet.xsd");

// Use XPath to query the name of the first staff member
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList list = root.SelectNodes ("//Staff/*", nsMgr);

// If the namespace prefix is **absent**,
// <MyData xmlns="http://www.mydomain.com/MyDataSet.xsd">
// the resulting list contains no nodes and this call fails, otherwise it suceeds.
result = list[0].Attributes["FirstName"].Value;
}
catch (Exception e)
{
result = "**failed**\n" + e.Message;
}
MessageBox.Show ("XPath query: " + result);
}

I'd like to use the prefix, but at this point (implying a modification to the dataset approach), I just want the darn thing to work.

In case it matters, my strongly-typed dataset was generated in this way:
1. Created the XML data file by hand
2. Generated the XML schema using VS tool
3. Fixed up the XML schema for a better dataset
Renamed "NewDataSet" to "MyDataSet" in the node and on the xmlschema id
Changed the targetnamespace to http://www.mydomain.com/MyDataSet.xsd
Added XmlNamespace http://www.mydomain.com/MyDataSet.xsd with qualifyer "md" to the xmlschema's Namespaces collection
4. Generated the MyDataSet class from the XML schema
Note that there is a Prefix member of the generated dataset which seems always to be the value "". With the namespace prefix in the input XML present, modifying this generated code to set the Prefix to "md" has no effect. The dataset query still fails.

Here's a snippet of the data:
<?xml version="1.0" encoding="utf-8"?>
<MyData xmlns="http://www.mydomain.com/MyDataSet.xsd">
<Staff>
<StaffMember FirstName="JoeBob" LastName="Briggs"/>
</Staff>
</MyData>

Again, any insights much appreciated.

-Kristin
 
D

Derek Harmon

Kristin said:
if the XML input file contains a namespace prefix, my XPath query
through the XmlDataDocument works great, but the corresponding
dataset is not loaded and the dataset query fails. : :
Conversely, if the XML input file has no namespace prefix, the
dataset query suceeds but the XPath query of the
XmlDataDocument fails.

Subsequently, you describe that there is a namespace URI
declared on the strongly-typed DataSet. I'm guessing that this
namespace declaration applies universally to the entire Data-
Set's instance document. In that case, I can see one reason
why the following might give a DataSet difficulty:

<MyData xmlns:md="http://www.mydomain.com/MyDataSet.xsd">

This is different from a MyData element containing an xmlns
default declaration, because this MyData is _not_ in the
namespace http://www.mydomain.com/MyDataSet.xsd.

If you want the prefix, then using the following XML might produce
more satisfactory results and be valid under the DataSet's schema:

<md:MyData xmlns:md="http://www.mydomain.com/MyDataSet.xsd">

This places MyData under the http://www.mydomain.com/MyDataSet.xsd
namespace instead of the empty namespace.


Derek Harmon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top