2011-01-13 43 views
1

我想寫以下到XML文檔中的Excel表格XML導出:使用XmlDocument的

<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"; 

如何防止這種情況發生?

+0

你爲什麼試圖自己編寫xml而不使用標準庫? – 2011-01-13 15:53:30

+0

我不知道有一個更簡單的方法。你有任何有用的鏈接? – Mark 2011-01-13 16:11:40

回答

2

使用XmlDocument.CreateElement過載,它允許你指定例如元素所需的前綴&命名空間

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); 

。希望清除的東西了你。

2

使用CreateElement的兩個參數版本,並將您的命名空間指定爲第二個參數。例如:

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