C# XML export for Excel table using XmlDocument

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2011-01-13T15:43:32Z Indexed on 2011/01/13 15:53 UTC
Read the original article Hit count: 266

Filed under:
|
|

I am trying to write the following into an XML document:

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

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?

Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml