【发布时间】: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