Hi,
I'm creating a component to generate a schema file much like VS generates for DataSets.
I'm using XmlSchema to do the job and I also am using a XmlNamespaceManager to manage namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("msdata", "urn:schemas-microsoft-com:xml-msdata");
nsmgr.AddNamespace("mstns", "http://tempuri.org/Dataset.xsd");
nsmgr.AddNamespace(string.Empty, "http://tempuri.org/Dataset.xsd");
The trouble is that when I write out the xsd using
schema.Write(writer, nsmgr);
It doesn't include the default namespace as it is included in xsds generated by VS. I can for the life of me get it to work.
Here is the root node output:
<xs:schema targetNamespace="http://tempuri.org/Dataset.xsd" elementFormDefault="qualified" attributeFormDefault="qualified" id="Dummy" xmlns:mstns="http://tempuri.org/Dataset.xsd" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema">
It makes some parts of my resultant dataset not validate and also VS does not recognise any relation elements.
I have implemented a workaround by manually adding it after the xsd is genereted as such:
doc.Load(filename);
XmlElement root = doc.DocumentElement;
XmlAttribute attrib = doc.CreateAttribute("xmlns");
attrib.Value = nsmgr.DefaultNamespace;
root.Attributes.Append(attrib);
XmlWriter writer = new XmlTextWriter(allTablesFileName, Encoding.UTF8);
doc.WriteTo(writer);
writer.Close();
This then matches the dataset xsd as such:
<xs:schema targetNamespace="http://tempuri.org/Dataset.xsd" elementFormDefault="qualified" attributeFormDefault="qualified" id="DataSet" xmlns:mstns="http://tempuri.org/Dataset.xsd" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/Dataset.xsd">
Now the xsd looks just like a dataset xsd, and validates and can be loaded in VS as normal.
Does any one know why the default namespace is not being included (it does if the prefixed namespace with the same namespace is not included in the namespace manager), and if there is a better workaround than I have done.
Thank you,
John