tags:

views:

6223

answers:

3

Hi,

How can I change one value (attribute) in xml file, using C#/

Thanks,

A: 

http://www.developer.com/net/csharp/article.php/3489611

IrishChieftain
FYI: It's usually considered bad practice to give an answer consisting of only a link. Typically people add a summary wit the link. Doesn't make much of a difference for me but some users will down vote for link only answers.
JaredPar
My friend, I know this link, I used it as well, but it didn't satisfy my needs, that's why I'm posting here, I use Google to before post on this site, so, please, if you have answer, post it, if not, save your links. Thanks,
Mike, if you didn't understand this link or it didn;t "satisfy" your needs. I'm sorry, but you probably should not be programming!
Strelok
@Mike I have to agree with the others on this one. What you're asking is a very basic thing, so it would behoove you to show us what you've tried before posting something negative about someone's answer.
George Stocker
THis was the first link that came back from Google when I used Mike's question verbatim as a search query in Google. If that didn't solve the problems, then Mike needs to be more specific. Lighten up guys, its Christmas :-)
IrishChieftain
@IrishChiefain, I was attempting to be helpful vs. critical.
JaredPar
+4  A: 

Using LINQ to xml if you are using framework 3.5:

using System.Xml.Linq;

XDocument xmlFile = XDocument.Load("books.xml");

var query = from c in xmlFile.Elements("catalog").Elements("book")

select c;

foreach (XElement book in query)

{

book.Attribute("attr1").Value = "MyNewValue";

}

xmlFile.Save("books.xml");

alexmac
+8  A: 

Mike; Everytime I need to modify an XML document I work it this way:

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

I hope you find it useful

El Padrino