3

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?

2 Answers 2

2

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/

Sign up to request clarification or add additional context in comments.

2 Comments

OK, I'll certainly have a look at that, but I'd still like to know how to do it manually with the .NET XML classes.
AODL is a .NET module of the OpenOffice.org ODF Toolkit project which can found here: odftoolkit.org/projects/aodl
0

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);
    }
}

1 Comment

Brilliant - thank you. And you're right about it not needing the ones that aren't used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.