update an xml attribute
-
Hello, 1st, sorry for such a simple question...well maybe simple I'm new to c# and especially xml. I'm using an XmlTextReader to loop through an xml document, using if statements to "stop" at certain nodes. once I stop at certain nodes, I need to update one of the attributes. How can I update a single attribute while (CashReader.Read()) { // Move to first element CashReader.MoveToElement(); if (CashReader.Name == "Shopper") { CashReader.MoveToAttribute(i); if (CTFReader.Value == frmLogin.vID) { i=2 CashReader.MoveToAttribute(i); //I NEED TO UPDATE SHOPPER HERE if (CashReader.Value == "") { //THIS IS WHERE I WANT TO INSERT THE CODE TO UPDATE THE ATTRIBUTE CTFReader.Close(); thank you in advance for the help JC
-
Hello, 1st, sorry for such a simple question...well maybe simple I'm new to c# and especially xml. I'm using an XmlTextReader to loop through an xml document, using if statements to "stop" at certain nodes. once I stop at certain nodes, I need to update one of the attributes. How can I update a single attribute while (CashReader.Read()) { // Move to first element CashReader.MoveToElement(); if (CashReader.Name == "Shopper") { CashReader.MoveToAttribute(i); if (CTFReader.Value == frmLogin.vID) { i=2 CashReader.MoveToAttribute(i); //I NEED TO UPDATE SHOPPER HERE if (CashReader.Value == "") { //THIS IS WHERE I WANT TO INSERT THE CODE TO UPDATE THE ATTRIBUTE CTFReader.Close(); thank you in advance for the help JC
I suggest using an XmlDocument and its .Load method instead.
-
I suggest using an XmlDocument and its .Load method instead.
ok, but if I load the document then I need to go to a certain location depending on the information a user enters in a textbox I tried the below code, but I get an error "The process cannot access the file 'file.xml' because it is being used by another process."
XmlDocument upd = new XmlDocument(); upd.Load("file.xml"); XmlAttribute AttrUpd = upd.CreateAttribute("value"); AttrUpd.Value = "35.00"; upd.Save(file.xml); //THIS IS WHERE I GET THE ERROR
thanks
-
ok, but if I load the document then I need to go to a certain location depending on the information a user enters in a textbox I tried the below code, but I get an error "The process cannot access the file 'file.xml' because it is being used by another process."
XmlDocument upd = new XmlDocument(); upd.Load("file.xml"); XmlAttribute AttrUpd = upd.CreateAttribute("value"); AttrUpd.Value = "35.00"; upd.Save(file.xml); //THIS IS WHERE I GET THE ERROR
thanks
nyjcr wrote:
XmlDocument upd = new XmlDocument(); upd.Load("file.xml"); XmlAttribute AttrUpd = upd.CreateAttribute("value"); AttrUpd.Value = "35.00"; upd.Save(file.xml); //THIS IS WHERE I GET THE ERROR
It works fine at my end. But your code won't add attribute. You are not appending the attribute to any of the node. So you don't find this attribute in the file. Use XPath or
upd.GetElementsByTagName()
to get node, and usenode.Attributes.Apped()
. CallSave()
then.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions