首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用XmlDocument实现Excel表格的XML导出

使用XmlDocument实现Excel表格的XML导出
EN

Stack Overflow用户
提问于 2011-01-13 23:43:32
回答 2查看 2.2K关注 0票数 1

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

代码语言:javascript
运行
复制
<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:‘。

代码语言:javascript
运行
复制
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";

我如何才能阻止这种情况的发生?

EN

Stack Overflow用户

回答已采纳

发布于 2011-01-13 23:51:31

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

代码语言:javascript
运行
复制
document.CreateElement("x", "ExcelWorkbook", "urn:schemas-microsoft-com:office:excel");

示例

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

代码语言:javascript
运行
复制
// 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);

希望这能帮你理清头绪。

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4681927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档