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=3D"
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=3D"
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. =20
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 =3D new Data.MyDataSet();
XmlDataDocument xmlDoc =3D new XmlDataDocument (ds);
// Load the XML
xmlDoc.Load (fileName);
=20
string result =3D null;
try=20
{
// Use the dataset to query the name of the first staff member
// If the namespace prefix is **present**,=20
// <MyData xmlns:md=3D"
http://www.mydomain.com/MyDataSet.xsd"> // the dataset is empty and this call fails. Otherwise, it =
suceeds.
result =3D ds.StaffMember[0].FirstName;
}
catch (Exception e)=20
{
result =3D "**failed**\n" + e.Message;
}
MessageBox.Show ("Dataset query: " + result);
try=20
{
//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsMgr =3D new =
XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace ("md", =
"
http://www.mydomain.com/MyDataSet.xsd");
nsMgr.AddNamespace (String.Empty, =
"
http://www.mydomain.com/MyDataSet.xsd"); =20
=20
// Use XPath to query the name of the first staff member
XmlElement root =3D xmlDoc.DocumentElement;
XmlNodeList list =3D root.SelectNodes ("//Staff/*", nsMgr); =20
// If the namespace prefix is **absent**,=20
// <MyData xmlns=3D"
http://www.mydomain.com/MyDataSet.xsd"> // the resulting list contains no nodes and this call fails, =
otherwise it suceeds.
result =3D list[0].Attributes["FirstName"].Value;
}
catch (Exception e)=20
{
result =3D "**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=3D"1.0" encoding=3D"utf-8"?>
<MyData xmlns=3D"
http://www.mydomain.com/MyDataSet.xsd">=20 <Staff>
<StaffMember FirstName=3D"JoeBob" LastName=3D"Briggs"/>
</Staff>
</MyData>
Again, any insights much appreciated.