Wednesday, 3 December 2014

How to can I specify namespace to an XAttribute while having another namespace with the same value?



All I want to do is an XML document for exporting my datatable to Excel.


So what I need is something like this:



<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?mso-application Excel.Sheet?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" />


I am using System.Xml.Linq and I almost have it, but my code keeps adding "ss" to the front of Workbook. This is my code:



XDocument xmlssDoc2 = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new
XProcessingInstruction("mso-application", "Excel.Sheet"));

XNamespace aw = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace fc = "urn:schemas-microsoft-com:office:spreadsheet";
XElement root = new XElement(aw + "Workbook",
new XAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet"),
new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet")
);


And the result I get is:



<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?mso-application Excel.Sheet?>
<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" />


Any Help please!


No comments:

Post a Comment