2011-09-28 29 views
1

我使用System.Xml來創建XML文檔。我需要創建一個類似於下面的內容:C#WriteAttributeString - 多個元素

<Communication primary="N" value="[email protected]" purpose="PERSONAL" type="EMAIL"/> 

我可以用「WriteAttributeString」就得到就象這樣:

<Communication primary="N"/> 

問題是,它不會讓我添加更多屬性我有點卡住了。任何幫助/建議將不勝感激。

編輯:代碼如下創建上述XML:

writer.WriteStartElement("CommunicationList"); 

writer.WriteStartElement("Communication"); 
writer.WriteAttributeString("primary", "N"); 
writer.WriteEndElement(); 

writer.WriteEndElement(); 

我需要添加「價值」,「目的」和「類型」屬性,這和我在一個損失。

感謝

+0

我們看一些代碼。 –

回答

4

這應該呈現你想要什麼:

writer.WriteStartElement("CommunicationList"); 

writer.WriteStartElement("Communication"); 
writer.WriteAttributeString("primary", "N"); 
writer.WriteAttributeString("value", "[email protected]"); 
writer.WriteAttributeString("purpose", "PERSONAL"); 
writer.WriteAttributeString("type", "EMAIL"); 
writer.WriteEndElement(); 

writer.WriteEndElement(); 
+0

這是完美的,我沒有去嘗試它,因爲我認爲它會給我四個獨立的標籤,而不是將它們連接成一個。謝謝。 – JMK