如何在 .NET 中创建 OpenOffice 文档

发布于 2024-08-22 06:33:16 字数 1898 浏览 5 评论 0原文

我开始编写一个输出 OpenOffice 文档的应用程序。空 ODT 文档中的 content.xml 文件开头为:

<?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">

我以前没有在 XML 中见过冒号。我知道它们表示名称空间。我该如何在 .NET 中重新创建它?

I am starting to write an application that will output an OpenOffice document. The content.xml file in an empty ODT document begins:

<?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">

I haven't seen colons in XML before. I understand that they signify namespaces. How do I go about recreating this in .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远山浅 2024-08-29 06:33:16

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

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

OpenOffice includes .NET assemblies for working with OpenOffice documents. You should use these instead of working with the XML directly. It'll be much easier and less error prone.

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

盛夏尉蓝 2024-08-29 06:33:16

以下代码会将所有正确的命名空间加载到命名空间管理器中并创建示例文档。 .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);
    }
}

The following code will load up all of the proper namespaces into a namespace manager and create a sample document. .NET will only output the namespaces that are actually used in the document though, not all of them. I'm not sure if there is a way to force output of the unused ones, but unused ones really shouldn't be needed.

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文