2016-03-02 74 views
0

我正在將代碼從vbs更改爲C#,但是我有一個XMLwriter問題,如何使用XML編寫在XML.file中獲取此類輸出:??如何使用XmlWriter在元素中編寫名稱空間

我有這樣的代碼:

using (XmlWriter writer = XmlWriter.Create(Form1.systemDrive + "\\unattend.xml")) 
{ 
    writer.WriteStartDocument(); 
    writer.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); 
    writer.WriteStartElement("unattend"); 
    writer.WriteAttributeString("xmlns", "urn:schemas-microsoft-com:unattend"); // <<<<<<< Gives an error 

....

+0

你能提供預期的產出嗎?你會得到什麼錯誤? – tdragon

+0

您需要[使用WriteAttributeString的不同重載](https://msdn.microsoft.com/en-us/library/73z46xs1(v = vs.110).aspx)編寫名稱空間聲明。 – stuartd

+0

「一個錯誤」總是對你得到的錯誤的一個很好的描述......: - /就像「不工作」一樣。 –

回答

0

如果你只想把無人蔘與到標籤的給定命名空間,完全不使用WriteAttributeString,只是使用

writer.WriteStartElement("unattend","urn:schemas-microsoft-com:unattend"); 

如果命名空間被分配給前綴,它將使用前綴,否則它將生成必要的xmlns子句。

此外,WriteStartDocumentWriteProcessingInstruction是多餘的。兩者都產生相同的輸出,但是你只能有一條處理指令,所以你可以使用其中一個,但不能同時使用兩個。

相關問題