Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. update an xml attribute

update an xml attribute

Scheduled Pinned Locked Moved C#
questioncsharpxmlhelpannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nyjcr
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • N nyjcr

      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

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I suggest using an XmlDocument and its .Load method instead.

      N 1 Reply Last reply
      0
      • P PIEBALDconsult

        I suggest using an XmlDocument and its .Load method instead.

        N Offline
        N Offline
        nyjcr
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • N nyjcr

          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

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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 use node.Attributes.Apped(). Call Save() then.

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups