Ok,
I have a typed DataSet that was generated from an xsd schema using xsd.exe. The xsd schema is owned by a standards organization, so I can't modify it. The generated code sets the Namespace property of the DataSet (e.g. http://www.std.org/xml/name-space). This controls the scope of elements read from an XML file when calling ReadXml and the file is read correctly with appropriate rows populated.
Now, if I turn around and call WriteXml the typed DataSet creates a root element based on the DataSetName property, which is Ok. However, I get the namespace attribute on this element. (e.g. <MyDataSet xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance" xmlns="http://www.std.org/xml/name-space">)
Now if I try to validate the resulting document against the original schema file I get a "Xml validation failed" ... " The 'http://www.std.org/xml/name-space:MyDataSet' element is not declared."
What would it take to resolve this issue

Typed DataSet and WriteXml question about Namespace
The CynicalDoctor
In a manner of speaking, yes. That's not the actual name, I was just using it as an example.
The schema goes something like this...
<xsd:element name="Information" type="InformationType"/>
<xsd:element name="Class" type="ClassType"/>
<xsd:element name="Instance" type="InstanceType"/>
<xsd:complexType name="InformationType">
<xsd:sequence>
<xsd:element name="Instance" type="InstanceType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Class" type="ClassType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="InstanceType">
...
</xsd:complexType>
<xsd:complexType name="ClassType">
...
</xsd:complexType>
So I end up with Tables named "Information", "Class", and "Instance".
ullfindsmit
Nice idea, but it doesn't really work. This is a typed DataSet, so the constructor method is only available if I override a new constructor.
I can set the DataSetName property, but it doesn't solve the problem.
MyDataSet mds = new MyDataSet();
mds.DataSetName = "rootElement";
This will just result in <rootElement xmlns="http://www.std.org/xml/name-space"> as the output of WriteXml(). So schema validation fails again with message "The 'http://www.std.org/xml/name-space:rootElement' is not declared."
Even if I set the DataSetName to the name of an element that would be valid for the schema, I then run into the issue where I could have 2 of these nested, which wouldn't be valid for the schema validation either.
e.g.
<schemaRootElementName xmlns="http://www.std.org/xml/name-space">
<schemaRootElementName>
<ID>abc</ID>
</schemaRootElementName>
</schemaRootElementName>
The xsd defines <schemaRootElementName> as containing an <ID> element, but NOT a nested <schemaRootElementName>. So we fail validation again.
What I need to be able to do is to not put the namepsace on the root element so that it does not get considered for validation by the schema, and have the namespace attribute on the element(s) that are part of the schema and should be checked during validation. (I think)
I've seen this issue posted elsewhere and rarely with a reply. When I have seen a reply, it usually involves modifying the xsd schema. But that's a non-starter for me because I don't own the schema. It is produced by a standards organization.
KarlFollman
I think I have exactly the same problem. It seem that the WriteXml() method would always wraps its dataset name as the root element around our root element. Here is what you can do away with it. (Assuming you are writing it to a file).
string s = @"E:\data\xmlroot\SampleData.xml";
// Get the xml data in string form
string xml = dataSet.GetXml();
// Create a writer of your own
XmlTextWriter writer = new XmlTextWriter(s, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
// Use the XmlDataDocument to write your dataset instead of DataSet
xmlDocument.WriteContentTo(writer);
writer.Close();
Gabriel M&#233;ndez
Thanks for the reply. I hope I can get this working.
Your example code is missing a few bits that I'd like to ask about.
1. What is "string xml..." doing I don't see it referenced after the dataSet.GetXml() call.
2. Where/How does xmlDocument (XmlDataDocument) get bound to the XML or DataSet
Thanks,
Nick
Ming Gao
Guto
Ahh, I see. That seems a little strange. I don't think Type DataSets are meant to be for round-trip with XML data.
You could try just copying the the value of the DataSet.Namespace property to all the tables before removing it from the DataSet, then generating the XML:
MyDataSet myDataSet = new MyDataSet();
myDataSet.DataSetName = "root";
//...
foreach (DataTable table in myDataSet.Tables)
{
table.Namespace = myDataSet.Namespace;
}
myDataSet.Namespace = "";
String xml = myDataSet.GetXml();
Peter Morgenthaler
It appears that changing the Namespace of the tables in the foreach loop is causing some issues regarding child tables and multiple nampspaces. (yeah, some tables are children of multiple tables :( ).
It's starting to look like I'd have to do
new public void WriteXml(...) seven times
Yuk! The supposed benefits of having WriteXml available was one of the reasons for going with typed DataSets.
Still searching for a solution.
Navdeep
You can set the DataSet.DataSetName property (or pass the name in the DataSet constructor, if you're constructing manually). It is that string this is used for the root element name. e.g.:
DataSet dataSet = new DataSet("rootElement");
DataSet dataSet = new DataSet();
//...
dataSet.DataSetName = "rootElement";
-- Peter
______
http://www.peterRitchie.com/Blog/
srinivasintouch
I am sorry that I am too excited to tell the solution without even understanding how my workaround works. Here is the entire description of how my work around works.
Please note that after some clarification, I realized this work around will only work if you retrieve data from or write data to xml files, if you are retrieving data from a database, I don't think it will work.
// First create a dataset, being a typed or generic doesn't matter
DataSet ds = new DataSet();
// Associate a XmlDataDocument with the dataset by its one-step constructor
XmlDataDocument xdd = new XmlDataDocument(ds);
// Since they are bound together, so people think they can either use
// DataSet.ReadXml() or XmlDataDocument.Load(), but these two methods are
// different and they create the problem you post. If you use DataSet.ReadXml(), the
// dataset will automatically wrap its dataset name around our top element. Therefore,
// we use the other one. However, I think you can also optionally attach the schema
// to the dataset
ds.ReadXmlSchema(schemaName);
xdd.Load(filename);
// Now you can modify your data by using the dataset, just an example here
DataRowView drv = ds.Tables["Student"].DefaultView[0];
drv["Age"] = 16;
drv.EndEdit();
// Now write back to the file, this operation, similar to read, will not let the dataset be
// involved in the writing process.
XmlTextWriter xtw = new XmlTextWriter(saveFileDialog.FileName, Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xdd.WriteContentTo(xtw);
xtw.Close();
// Now check the file you saved
So in general, you read and write the data using XmlDataDocument, but you modify the data as though modifying data in a database by using DataSet.
I do not know whether your problem is a bug in .NET or .NET design it in such a way that DataSet must be wrapped with its name, but I hope this can work on your program. I work on mine anyway. In case if it is still not working, then it is probably because the misunderstanding of your problem.