Martin,
>Semantically there is no difference between the two snippets, it does not matter in terms of XML and namespaces whether a namespace is used as
the default namespace or with a prefix.
Unfortunately, it appears to matter a great deal to Microsoft Excel. In testing on both Excel XP and Excel 2007, not having that
xmlns="..." attribute prevents the file from being parsed. Personally, and not knowing much about XML, I think having two attributes
that point at the same namespace (i.e., xmlns="..." and xmlns:ss="<same>") strikes me as weird...but apparently not to the folks who
wrote Excel :).
>Can you show us the relevant C# or VB.NET class definition of the objects you serialize? There are attributes that set the namespace and
the Serialize method has a third argument that maps prefixes to namespace URI so this class
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="urn:schemas-microsoft-com:office:spreadsheet", IsNullable=false)]
public partial class Workbook
{
}
and
XmlSerializer serializer = new XmlSerializer(typeof(Workbook));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("x", "urn:schemas-microsoft-com:office:excel");
ns.Add("c", "urn:schemas-microsoft-com:office:component");
ns.Add("html", "http://www.w3.org/TR/REC-html40");
ns.Add("ss", "urn:schemas-microsoft-com:office:spreadsheet");
serializer.Serialize(tw, this, ns); // this is a Workbook object
generates:
<?xml version="1.0" encoding="utf-8"?>
<ss:Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:c="urn:schemas-microsoft-com:office:component" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="Sheet1">
which doesn't work because the default xmlns tag is missing. Not adding the "ss" namespace to namespaces generates:
<?xml version="1.0" encoding="utf-8"?>
<Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:c="urn:schemas-microsoft-com:office:component" xmlns="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet d2p1:Name="Sheet1" xmlns:d2p1="urn:schemas-microsoft-com:office:spreadsheet" xmlns="">
which doesn't work because the Worksheet element redefines the default namespace to "". FYI, the Worksheet class definition is as
follows:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType=true, Namespace="urn:schemas-microsoft-com:office:spreadsheet")]
public partial class WorkbookWorksheet
{
}
Adding the named tag ElementName="Workbook" to the XmlRootAttribute while not adding the ss namespace to the serializer generates
<?xml version="1.0" encoding="utf-8"?>
<Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:c="urn:schemas-microsoft-com:office:component" xmlns="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet d2p1:Name="Sheet1" xmlns:d2p1="urn:schemas-microsoft-com:office:spreadsheet" xmlns="">
which suppresses the ss: prefix on the Workbook node, but still has the problem of the Worksheet node redefining xmlns.
Adding the Namespace= parameter to the XmlTypeAttribute on WorkbookWorksheet:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xs d", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("c ode")]
[System.Xml.Serialization.XmlTypeAttribute(Anonymou sType = true, Namespace = "urn:schemas-microsoft-com:office:spreadsheet")]
public partial class WorkbookWorksheet
{
}
generates
<?xml version="1.0" encoding="utf-8"?>
<Workbook xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:c="urn:schemas-microsoft-com:office:component" xmlns="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet d2p1:Name="Sheet1" xmlns:d2p1="urn:schemas-microsoft-com:office:spreadsheet" xmlns="">
again with the redefinition of the default namespace on the Worksheet node.
The schema where Workbook and Worksheet are defined looks like this (some hopefully unrelated details left out for brevity):
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:c="urn:schemas-microsoft-com:office:component" attributeFormDefault="unqualified"
targetNamespace="urn:schemas-microsoft-com:office:spreadsheet" elementFormDefault="unqualified">
<xs:import namespace="urn:schemas-microsoft-com:office:office" />
<xs:import namespace="urn:schemas-microsoft-com:office:excel" />
<xs:import namespace="urn:schemas-microsoft-com:office:component" />
<xs:element name="Workbook">
<xs:complexType>
<xs:sequence>
<xs:element name="Styles">
</xs:element>
<xs:element maxOccurs="unbounded" name="Worksheet">
<xs:complexType>
<xs:sequence>
</xs:sequence>
<xs:attribute ref="ss:Name" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
....
I appreciate your help, and look forward to other suggestions.
- Mark