2010-02-16 85 views
3

我开始编写将输出OpenOffice文档的应用程序。在一个空的ODT文档中的content.xml文件开始:如何在.NET中创建OpenOffice文档

<?xml version="1.0" encoding="UTF-8"?> 
<office:document-content 
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" 
    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
    xmlns:math="http://www.w3.org/1998/Math/MathML" 
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" 
    xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" 
    xmlns:ooo="http://openoffice.org/2004/office" 
    xmlns:ooow="http://openoffice.org/2004/writer" 
    xmlns:oooc="http://openoffice.org/2004/calc" 
    xmlns:dom="http://www.w3.org/2001/xml-events" 
    xmlns:xforms="http://www.w3.org/2002/xforms" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:rpt="http://openoffice.org/2005/report" 
    xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" 
    xmlns:rdfa="http://docs.oasis-open.org/opendocument/meta/rdfa#" 
    xmlns: 
     field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" 
    office:version="1.2"> 

我以前没见过冒号。我明白他们表示命名空间。我如何去重新创建这个在.NET中?

回答

0

下面的代码将所有适当的命名空间的装载到一个命名空间管理,并创建一个样本文件。 .NET将仅输出文档中实际使用的名称空间,但不是全部。我不确定是否有办法强制未使用的产品的输出,但真正不需要使用未使用的产品。

public static void Test() 
{ 
    var nt = new NameTable(); 
    var ns = new XmlNamespaceManager(nt); 
    var doc = new XmlDocument(nt); 

    ns.AddNamespace("office", "urn:oasis:names:tc:opendocument:xmlns:office:1.0"); 
    ns.AddNamespace("style", "urn:oasis:names:tc:opendocument:xmlns:style:1.0"); 
    ns.AddNamespace("text", "urn:oasis:names:tc:opendocument:xmlns:text:1.0"); 
    ns.AddNamespace("table", "urn:oasis:names:tc:opendocument:xmlns:table:1.0"); 
    ns.AddNamespace("draw", "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"); 
    ns.AddNamespace("fo", "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"); 
    ns.AddNamespace("xlink", "http://www.w3.org/1999/xlink"); 
    ns.AddNamespace("dc", "http://purl.org/dc/elements/1.1/"); 
    ns.AddNamespace("meta", "urn:oasis:names:tc:opendocument:xmlns:meta:1.0"); 
    ns.AddNamespace("number", "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"); 
    ns.AddNamespace("svg", "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"); 
    ns.AddNamespace("chart", "urn:oasis:names:tc:opendocument:xmlns:chart:1.0"); 
    ns.AddNamespace("dr3d", "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"); 
    ns.AddNamespace("math", "http://www.w3.org/1998/Math/MathML"); 
    ns.AddNamespace("form", "urn:oasis:names:tc:opendocument:xmlns:form:1.0"); 
    ns.AddNamespace("script", "urn:oasis:names:tc:opendocument:xmlns:script:1.0"); 
    ns.AddNamespace("ooo", "http://openoffice.org/2004/office"); 
    ns.AddNamespace("ooow", "http://openoffice.org/2004/writer"); 
    ns.AddNamespace("oooc", "http://openoffice.org/2004/calc"); 
    ns.AddNamespace("dom", "http://www.w3.org/2001/xml-events"); 
    ns.AddNamespace("xforms", "http://www.w3.org/2002/xforms"); 
    ns.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema"); 
    ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
    ns.AddNamespace("rpt", "http://openoffice.org/2005/report"); 
    ns.AddNamespace("of", "urn:oasis:names:tc:opendocument:xmlns:of:1.2"); 
    ns.AddNamespace("rdfa", "http://docs.oasis-open.org/opendocument/meta/rdfa#"); 
    ns.AddNamespace("field", "urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0"); 

    var root = doc.CreateElement("office", "document-content"); 
    var attr = doc.CreateAttribute("office", "version", ns.LookupNamespace("office")); 
    attr.Value = "1.2"; 
    root.Attributes.Append(attr); 
    doc.AppendChild(root); 

    using (var xw = new XmlTextWriter(Console.Out)) 
    { 
     xw.Formatting = Formatting.Indented; 
     xw.Indentation = 2; 
     xw.IndentChar = ' '; 

     doc.WriteTo(xw); 
    } 
} 
+0

辉煌 - 谢谢。你说得对,不需要那些没有使用的。 – matthewk 2010-02-17 18:24:44

2

OpenOffice包含用于处理OpenOffice文档的.NET程序集。您应该使用这些而不是直接使用XML。它会容易得多,而且容易出错。

http://opendocument4all.com/content/view/68/47/

+0

好的,我一定会看看,但我仍然想知道如何使用.NET XML类手动执行此操作。 – matthewk 2010-02-16 16:05:37

+0

AODL是OpenOffice.org ODF Toolkit项目的.NET模块,可在此处找到:http://odftoolkit.org/projects/aodl – Alex 2010-05-08 16:12:57