【问题标题】:XML export for Excel table using XmlDocument使用 XmlDocument 导出 Excel 表的 XML
【发布时间】:2011-01-14 19:58:37
【问题描述】:

我正在尝试将以下内容写入 XML 文档:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

    <head>
     <xml> 
      <x:ExcelWorkbook>
       <x:ExcelWorksheets>
        <x:ExcelWorksheet>
         other code here
        </x:ExcelWorksheet>
       </x:ExcelWorksheets>
      </x:ExcelWorkbook>
     </xml>
    </head>
   </html> 

但是,如果我使用以下代码,它会去掉“x:”。

System.Xml.XmlDocument document = new System.Xml.XmlDocument();

System.Xml.XmlElement htmlNode = document.CreateElement("html");
htmlNode.SetAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office");
htmlNode.SetAttribute("xmlns:x", "urn:schemas-microsoft-com:office:excel");
htmlNode.SetAttribute("xmlns", "http://www.w3.org/TR/REC-html40");
document.AppendChild(htmlNode);

System.Xml.XmlElement headNode = document.CreateElement("head");
htmlNode.AppendChild(headNode);
headNode.AppendChild(
  document.CreateElement("xml")).AppendChild(
  document.CreateElement("x:ExcelWorkbook"))).AppendChild(
  document.CreateElement("x:ExcelWorksheets")).AppendChild(
  document.CreateElement("x:ExcelWorksheet")).InnerText="other code here";

我怎样才能阻止这种情况发生?

【问题讨论】:

  • 你为什么要自己写xml而不使用标准库?
  • 我不知道还有更简单的方法。你有什么有用的链接吗?
  • @Yuriy:我想不出任何库比@Mark 已经在做的更容易......

标签: c# xml xmldocument


【解决方案1】:

使用XmlDocument.CreateElement 重载,它允许您指定元素所需的前缀和命名空间,例如

document.CreateElement("x", "ExcelWorkbook", "urn:schemas-microsoft-com:office:excel");

示例

根据您的评论,我认为您已经按照我建议的方式创建了 每个 元素。我指的是如何让您的 Excel 特定元素具有正确的命名空间前缀。请参阅下面的示例,了解如何正确地将每个元素与正确的命名空间前缀相关联:

// store ns in a local variable as it is going to be re-used
const string HTML_NS = "http://www.w3.org/TR/REC-html40";
const string EXCEL_NS = "urn:schemas-microsoft-com:office:excel";

XmlDocument doc = new XmlDocument();
// create the HTML element and set the HTML namespace as the default
XmlElement htmlElement = doc.CreateElement("html", HTML_NS);
// add the office/excel namespaces into the HTML element (so we can reference them later)
htmlElement.SetAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office");
htmlElement.SetAttribute("xmlns:x", EXCEL_NS);
doc.AppendChild(htmlElement);
// associate the HEAD element with the HTML namespace as this is a HTML element
XmlElement headElement = doc.CreateElement("head", HTML_NS);    
htmlElement.AppendChild(headElement);
// now this is the one I am not too sure about as I don't know which 
// namespace you would associate the XML tag with. If you run the code 
// as it is specified here it will write out as <xml xmlns=""> which is
// correct as we aren't associating it with a namespace. As a workaround 
// you could associate it with the HTML NS if it is just for writing out 
XmlElement xmlElement = doc.CreateElement("xml", HTML_NS);   
headElement.AppendChild(xmlElement);
// create ExcelWorkbook element and associate with the excel namespace
// Unlike the HTML/HEAD tags we need to supply the prefix here because the Excel
// namespace is not the default
XmlElement xlWorkbookElement = doc.CreateElement("x", "ExcelWorkbook", EXCEL_NS);
xmlElement.AppendChild(xlWorkbookElement);
// create ExcelWorksheets element and associate with the excel namespace
XmlElement xlWorksheetsElement = doc.CreateElement("x", "ExcelWorksheets", EXCEL_NS);
xlWorkbookElement.AppendChild(xlWorksheetsElement);
// create the ExcelWorksheet element and associate with the excel namespace
XmlElement xlWorksheetElement = doc.CreateElement("x", "ExcelWorksheet", EXCEL_NS);
xlWorksheetsElement.AppendChild(xlWorksheetElement);

希望能为您解决问题。

【讨论】:

  • 谢谢,但是如果 URN 部分已经在 标记中声明,它会在每个条目中添加额外的 xmlns:x="URN..." 吗?
  • @Mark:只要您像示例所示那样定义前缀,它就不会将命名空间添加到每个元素。您需要提供命名空间,以便元素知道前缀所指的命名空间。
  • 这非常接近工作 - 但有两个问题:1)第一个标签现在变成 x:html 和 2)每个子元素现在在开始标签内都有 xmlns=""。你知道如何纠正这些吗? (主要是第一个!)
【解决方案2】:

使用 CreateElement 的双参数版本,并将您的命名空间指定为第二个参数。例如:

System.Xml.XmlElement myElement = document.CreateElement(
                                      "x:ExcelWorkbook",
                                      "urn:schemas-microsoft-com:office:excel");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多