我开始编写一个输出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中重新创建它?
答案 0 :(得分:2)
OpenOffice包含用于处理OpenOffice文档的.NET程序集。您应该使用这些而不是直接使用XML。它会更容易,更不容易出错。
答案 1 :(得分: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);
}
}