2012-03-26 67 views
0

我正在C#winform項目上工作,我正在解析我的應用程序中的一些xml。當我檢查一些條件時,我正在嘗試更改屬性值,但出現一些錯誤。這裏是我的代碼:C#替換已存在的xml屬性

If(mycondition){ 
writer.WriteAttributeString("type","loopTask"); 
} 

我不得不提到屬性「類型」在我的xml文件已經存在,我得到的錯誤「類型」是重複的屬性名稱。我怎樣才能取代價值?什麼是最簡單的方法來完成這項任務?

+0

你能表明你工作的一個示例XML文件? – 2012-03-26 09:49:49

回答

1

一個改變屬性的方式可以是:

//Here is the variable with which you assign a new value to the attribute 
string newValue = string.Empty 
XmlDocument xmlDoc = new XmlDocument(); 

xmlDoc.Load(xmlFile); 

XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element"); 
node.Attributes[0].Value = newValue; 

xmlDoc.Save(xmlFile); 

//xmlFile is the path of your file to be modified