使用 XmlDocument 将 XML 导出为 Excel 表
我正在尝试将以下内容写入 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";
我怎样才能阻止这种情况发生?
I am trying to write the following into an XML document:
<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>
However, if I use the following code, it strips out the '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";
How can I stop this from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 XmlDocument.CreateElement 重载,它允许您指定所需的前缀 & ;元素的命名空间,例如
示例
从您的评论中,我认为您已经按照我建议的方式创建了每个元素。我指的是如何让 Excel 特定元素具有正确的命名空间前缀。请参阅下面的示例,了解如何将每个元素与正确的名称空间前缀正确关联:
希望这能为您解决问题。
Use the XmlDocument.CreateElement overload which allows you to specify the required prefix & namespace of the element e.g.
Example
From your comment I think you have went ahead and create every element in the way I have suggested. What I was referring to was how to get your Excel specific elements to have the correct namespace prefix. See my example below to see how to properly associate each of your elements with the correct namespace prefix:
Hope that clears things up for you.
使用 CreateElement 的双参数版本,并将您的命名空间指定为第二个参数。例如:
Use the two argument version of CreateElement, and specify your namespace as the second argument. For example: