Reputation: 79
how I can change the attribute "id" using my source code?
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader(@"C:\Users\1.xml");
XmlNodeList elementList = reader.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element
{
reader.ReadToFollowing("command");
reader.MoveToAttribute("id");
Console.Write(reader.Value);
Console.WriteLine(" ");
}
break;
}
}
Console.Read();
}
I saw some examples, but they have used another methods that don't work with mine. (I'm a noobie)
Upvotes: 1
Views: 3833
Reputation: 61469
Code above didn't fly for me
this works thought
var doc = XDocument.Load(path);
foreach(var element in doc.Descendants("command"))
{
element.Attribute("id").Value = id;
}
doc.Save(path);
Hope this saves you some time.
Upvotes: 1
Reputation: 32807
I would use LINQ to XML
XElement doc=XDocument.Load(path);
foreach(var element in doc.Descendants().Elements("command"))
{
element.Attribute("id").Value=yourValue;
}
doc.Save(path);
This would change each command element's id attribute
Upvotes: 1