簡體   English   中英

如何使用XmlWriter在元素中編寫名稱空間

[英]How to write a namespace in an Element with XmlWriter

我將代碼從vbs更改為C#,但是我對XMLwriter有問題,如何使用XML write在XML.file中獲取這樣的輸出:

我有這樣的代碼:

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 

....

如果您只想將無人參與標簽放入給定的名稱空間,則完全不要使用WriteAttributeString ,而只需使用

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

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

另外, WriteStartDocumentWriteProcessingInstruction一起是多余的。 兩者都產生相同的輸出,但是您只能有一個處理指令,因此可以使用其中一個,但不能同時使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM