简体   繁体   English

Excel 2007:XML:对缺少的ss:名称空间限定符感到cho

[英]Excel 2007: XML: choking on missing ss: namespace qualifier

Trying to create an Excel 2007 XML file, using C# and System.XML.Serialization. 尝试使用C#和System.XML.Serialization创建Excel 2007 XML文件。 The root element of an Excel XML file is such: Excel XML文件的根元素如下所示:

<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". 默认名称空间中的许多元素都具有用“ ss”限定的属性,这是不必要的,因为“ ss”和默认名称空间都为“ urn:schemas-microsoft-com:office:spreadsheet”。

For example the Style element, appears as: 例如,Style元素显示为:

   <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: 创建XmlSerializerNamespaces时,我添加了Excel要的所有命名空间:

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'. 在生成的XML中,省略了“”(默认)名称空间,因为它与“ 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; 我的样式对象类似于:[XmlRoot(Namespace =“ urn:schemas-microsoft-com:office:spreadsheet”)]公共类样式{[XmlAttribute]公共字符串ID {get; set; 组; } }

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

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

When I actually generate the XML I am getting: 当我实际生成XML时,我得到:

<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. Excel不在乎元素上的ss:限定符,但它会阻塞,因为它不能使用Style的'ID'属性。 It must be hardcoded to look for the literal string 'ss:ID'? 必须使用硬编码查找文字字符串“ ss:ID”吗?

If I omit the 'ss' from the namespaces it produces cleaner XML, but does not make my error go away. 如果我从名称空间中省略“ s”,它将生成更清晰的XML,但不会使我的错误消失。 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. 我也尝试过省略'ss'命名空间,然后将Style对象的命名空间设置为字面上的'ss',但是它会生成一些名为'd4p1'的临时命名空间,并造成了真正的混乱。

Any ideas on how to make Excel read valid XML? 关于如何使Excel读取有效XML的任何想法?

And for bonus marks, how can I make the begginning of my XML file look like this: 对于加分标记,我该如何使XML文件的开头看起来像这样:

<?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? 显然,第一部分是XML编写器附带的,但是是否有任何非骇客的方法来获得第二行?

Thanks, ~S 谢谢〜S

Rather than writing the xml yourself, did you consider using the Open XML SDK 2.0 for Microsoft Office ? 您是否考虑使用针对Microsoft OfficeOpen XML SDK 2.0,而不是自己编写xml? 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: 除了帮助处理Open XML文档之外,它还包含许多文档,并且看起来非常适合您的任务:

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. 适用于Microsoft Office的Open XML SDK 2.0建立在System.IO.Packaging API的基础上,并提供了用于操作Open XML文档的强类型零件类。 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. 该SDK还使用.NET Framework语言集成查询(LINQ)技术来提供对Open XML文档内部的XML内容的强类型对象访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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