简体   繁体   English

C#替换已经存在的xml属性

[英]C# Replace xml attribute that already exists

I am working on a C# winform project and I am parsing some xml in my application. 我正在研究C#winform项目,并且正在解析应用程序中的xml。 When i am checking some conditions i am trying to change an attribute value but i get some error. 当我检查某些条件时,我试图更改属性值,但出现一些错误。 Here is my code: 这是我的代码:

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

i have to mention that the attribute "type" exists already in my xml file and i get the error 'type' is a duplicate attribute name. 我不得不提到,属性“类型”已经存在于我的xml文件中,并且我得到的错误“类型”是重复的属性名称。 How can i replace the value?. 我该如何替换值? What is the easiest way to achieve this task ?. 完成此任务的最简单方法是什么?

One way of changing attributes could be: 更改属性的一种方法是:

//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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM