C# 使用XmlDocument导出Excel表的XML

C# 使用XmlDocument导出Excel表的XML,c#,xml,xmldocument,C#,Xml,Xmldocument,我正在尝试将以下内容写入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:

我正在尝试将以下内容写入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> 

如何阻止这种情况发生?

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

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

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

System.Xml.XmlElement myElement = document.CreateElement(
                                      "x:ExcelWorkbook",
                                      "urn:schemas-microsoft-com:office:excel");
使用重载可以指定元素所需的前缀和名称空间,例如

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);
//在将要重复使用的局部变量中存储ns
常量字符串HTML\u NS=”http://www.w3.org/TR/REC-html40";
const string EXCEL\u NS=“urn:schemas-microsoft-com:office:EXCEL”;
XmlDocument doc=新的XmlDocument();
//创建HTML元素并将HTML名称空间设置为默认名称空间
XmlElement htmlElement=doc.CreateElement(“html”,html\n);
//将office/excel名称空间添加到HTML元素中(以便我们以后可以引用它们)
SetAttribute(“xmlns:o”,“urn:schemas-microsoft-com:office:office”);
SetAttribute(“xmlns:x”,EXCEL_NS);
附录子文档(htmlElement);
//将HEAD元素与HTML名称空间关联,因为这是一个HTML元素
XmlElement headElement=doc.CreateElement(“head”,HTML\n);
htmlElement.AppendChild(headElement);
//这是我不太确定的一个,因为我不知道是哪一个
//将XML标记与之关联的命名空间。如果你运行代码
//正如这里所规定的,它将写为
//正确,因为我们没有将其与命名空间关联。作为解决办法
//如果只是为了写出来,您可以将它与HTMLNS相关联
xmlement xmlement=doc.CreateElement(“xml”,HTML\n);
headElement.AppendChild(xmlElement);
//创建excel工作簿元素并与excel命名空间关联
//与HTML/HEAD标记不同,我们需要在此处提供前缀,因为Excel
//命名空间不是默认名称空间
XmlElement xlWorkbookElement=doc.CreateElement(“x”,“EXCEL工作簿”,EXCEL\n);
AppendChild(xlWorkbookElement);
//创建excel工作表元素并与excel命名空间关联
XmlElement xlWorksheetElement=doc.CreateElement(“x”,“ExcelWorksheets”,EXCEL\n);
xlWorkbookElement.AppendChild(xlWorksheetElement);
//创建excel工作表元素并与excel命名空间关联
XmlElement xlWorksheetElement=doc.CreateElement(“x”,“EXCEL工作表”,EXCEL\n);
xlWorksheetElement.AppendChild(xlWorksheetElement);
希望这能帮您解决问题。

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

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);
//在将要重复使用的局部变量中存储ns
常量字符串HTML\u NS=”http://www.w3.org/TR/REC-html40";
const string EXCEL\u NS=“urn:schemas-microsoft-com:office:EXCEL”;
XmlDocument doc=新的XmlDocument();
//创建HTML元素并将HTML名称空间设置为默认名称空间
XmlElement htmlElement=doc.CreateElement(“html”,html\n);
//将office/excel名称空间添加到HTML元素中(以便我们以后可以引用它们)
SetAttribute(“xmlns:o”,“urn:schemas-microsoft-com:office:office”);
SetAttribute(“xmlns:x”,EXCEL_NS);
附录子文档(htmlElement);
//将HEAD元素与HTML名称空间关联,因为这是一个HTML元素
XmlElement headElement=doc.CreateElement(“head”,HTML\n);
htmlElement.AppendChild(headElement);
//这是我不太确定的一个,因为我不知道是哪一个
//将XML标记与之关联的命名空间。如果你运行代码
//正如这里所规定的,它将写为
//正确,因为我们没有将其与命名空间关联。作为解决办法
//如果只是为了写出来,您可以将它与HTMLNS相关联
xmlement xmlement=doc.CreateElement(“xml”,HTML\n);
headElement.AppendChild(xmlElement);
//创建excel工作簿元素并与excel命名空间关联
//与HTML/HEAD标记不同,我们需要在此处提供前缀,因为Excel
//命名空间不是默认名称空间
XmlElement xlWorkbookElement=doc.CreateElement(“x”,“EXCEL工作簿”,EXCEL\n);
AppendChild(xlWorkbookElement);
//创建excel工作表元素并与excel命名空间关联
XmlElement xlWorksheetElement=doc.CreateElement(“x”,“ExcelWorksheets”,EXCEL\n);
xlWorkbookElement.AppendChild(xlWorksheetElement);
//创建excel工作表元素并与excel命名空间关联
XmlElement xlWorksheetElement=doc.CreateElement(“x”,“EXCEL工作表”,EXCEL\n);
xlWorksheetElement.AppendChild(xlWorksheetElement);

希望这能帮你解决问题。

你为什么要自己编写xml而不使用标准库?我不知道还有更简单的方法。你有什么有用的链接吗?@Yuriy:我想不出有哪个库比@Mark已经在做的事情更简单了……你为什么要自己编写xml而不使用标准库?我不知道还有更简单的方法。你有什么有用的链接吗?@Yuriy:我想不出有哪个库比@Mark已经在做的更简单了……谢谢,但是如果标签中已经声明了URN部分,它会在每个条目中添加额外的xmlns:x=“URN…”吗?@Mark:I