Hi,
How can I change one value (attribute) in xml file, using C#/
Thanks,
From stackoverflow
-
http://www.developer.com/net/csharp/article.php/3489611
JaredPar : 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.Strelok : 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!George Stocker : @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.IrishChieftain : 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 :-)JaredPar : @IrishChiefain, I was attempting to be helpful vs. critical. -
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");
-
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 modifiedI hope you find it useful
0 comments:
Post a Comment