简体   繁体   中英

Excel 2007: XML: choking on missing ss: namespace qualifier

Trying to create an Excel 2007 XML file, using C# and System.XML.Serialization. The root element of an Excel XML file is such:

<Workbook xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="urn:schemas-microsoft-com:office:spreadsheet">

Many elements within the default namespace, have attributes qualified with 'ss', which is unnecessary as both 'ss' and the default namespace are both "urn:schemas-microsoft-com:office:spreadsheet".

For example the Style element, appears as:

   <Style ss:ID="s21">
      <Font x:Family="Swiss" ss:Bold="1"/>
   </Style>

When I create my XmlSerializerNamespaces, I add all the namespaces, that Excel wants:

var ns = new XmlSerializerNamespaces();
ns.Add("", "urn:schemas-microsoft-com:office:spreadsheet");
ns.Add("o", "urn:schemas-microsoft-com:office:office");
ns.Add("x", "urn:schemas-microsoft-com:office:excel");
ns.Add("ss", "urn:schemas-microsoft-com:office:spreadsheet");
ns.Add("html", "http://www.w3.org/TR/REC-html40");

In the generated XML, the "" (default) namespace is omitted because it is the same as 'ss'.

My style object is something along the lines of: [XmlRoot(Namespace = "urn:schemas-microsoft-com:office:spreadsheet")] public class Styles { [XmlAttribute] public string ID { get; set; }

    [XmlAttribute]
    public string Name { get; set; }

    [XmlElement]
    public string Font { get; set; }
}

When I actually generate the XML I am getting:

<ss:Style ID="s21">
          <ss:Font x:Family="Swiss" Bold="1"/>
</ss:Style>

I don't think there is anything technically wrong with this. Excel doesn't mind the ss: qualifier on the elements, but it chokes because it can't the 'ID' attribute of Style. It must be hardcoded to look for the literal string 'ss:ID'?

If I omit the 'ss' from the namespaces it produces cleaner XML, but does not make my error go away. I have also tried omitting the 'ss' namespace, and then setting the namespace of the Style object to be literally 'ss', but it generates some temporary namespace named 'd4p1' and makes a real mess.

Any ideas on how to make Excel read valid XML?

And for bonus marks, how can I make the begginning of my XML file look like this:

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet"?>

Obviously the first part comes with an XML writer, but is there any non-hackish ways to get that second line?

Thanks, ~S

Rather than writing the xml yourself, did you consider using the Open XML SDK 2.0 for Microsoft Office ? Besides helping with the manipulation of Open XML docs, it also contains quite a bit of documentation, and seems like it would be well suited for your task:

The Open XML SDK 2.0 for Microsoft Office is built on top of the System.IO.Packaging API and provides strongly typed part classes to manipulate Open XML documents. The SDK also uses the .NET Framework Language-Integrated Query (LINQ) technology to provide strongly typed object access to the XML content inside the parts of Open XML documents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM