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. XML / XSL
  4. Help editing Xml file in C#

Help editing Xml file in C#

Scheduled Pinned Locked Moved XML / XSL
helpcsharpdatabasexmlannouncement
5 Posts 2 Posters 12 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.
  • J Offline
    J Offline
    jadughar
    wrote on last edited by
    #1

    Hi I'm trying to modify an Xml file. This is the content of the Xml. The problem is that after changes, it will creates a copy of the content of the Xml file without changing the old content. Before changes :

    After changes :

    The code is :

        //xml document object
        System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
        
        FileStream reader = new FileStream(this.\_XmlFilePath, FileMode.Open, FileAccess.ReadWrite);
    
        xdoc.XmlResolver = null;
        
        //load the xml file
        xdoc.Load(reader);
    
        System.Xml.XmlElement Node = (System.Xml.XmlElement)xdoc.DocumentElement.SelectSingleNode("//configuration/connectionStrings/add\[@name=\\"Application.Properties.Settings.DBConnectionString\\"\]");
        if (Node != null)
        {
            //key found, set the value
            Node.Attributes.GetNamedItem("Source").Value = "C:\\DataDabe\\DB.mdb";
    
            //finally, save the new version of the config file
            xdoc.Save(reader);
            reader.Close();
        }
    

    "For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras

    X 1 Reply Last reply
    0
    • J jadughar

      Hi I'm trying to modify an Xml file. This is the content of the Xml. The problem is that after changes, it will creates a copy of the content of the Xml file without changing the old content. Before changes :

      After changes :

      The code is :

          //xml document object
          System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
          
          FileStream reader = new FileStream(this.\_XmlFilePath, FileMode.Open, FileAccess.ReadWrite);
      
          xdoc.XmlResolver = null;
          
          //load the xml file
          xdoc.Load(reader);
      
          System.Xml.XmlElement Node = (System.Xml.XmlElement)xdoc.DocumentElement.SelectSingleNode("//configuration/connectionStrings/add\[@name=\\"Application.Properties.Settings.DBConnectionString\\"\]");
          if (Node != null)
          {
              //key found, set the value
              Node.Attributes.GetNamedItem("Source").Value = "C:\\DataDabe\\DB.mdb";
      
              //finally, save the new version of the config file
              xdoc.Save(reader);
              reader.Close();
          }
      

      "For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras

      X Offline
      X Offline
      Xmen Real
      wrote on last edited by
      #2

      yes it suppose to do, because you are saving the xml in same stream. try this

          if (Node != null)
          {
              //key found, set the value
              Node.Attributes.GetNamedItem("Source").Value = "C:\\DataDabe\\DB.mdb";
      
              //finally, save the new version of the config file
              **_reader.Position = 0;_**
              xdoc.Save(reader);
              reader.Close();
          }
      

      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

      ----------------------------------------------- 128 bit encrypted signature, crack if you can

      J 1 Reply Last reply
      0
      • X Xmen Real

        yes it suppose to do, because you are saving the xml in same stream. try this

            if (Node != null)
            {
                //key found, set the value
                Node.Attributes.GetNamedItem("Source").Value = "C:\\DataDabe\\DB.mdb";
        
                //finally, save the new version of the config file
                **_reader.Position = 0;_**
                xdoc.Save(reader);
                reader.Close();
            }
        

        TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

        ----------------------------------------------- 128 bit encrypted signature, crack if you can

        J Offline
        J Offline
        jadughar
        wrote on last edited by
        #3

        hi thanks for the answer..it partially solved the problem. now the result is :

        tem.Data.OleDb" />

        this part is wrong :

        tem.Data.OleDb" />

        "For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras

        X 1 Reply Last reply
        0
        • J jadughar

          hi thanks for the answer..it partially solved the problem. now the result is :

          tem.Data.OleDb" />

          this part is wrong :

          tem.Data.OleDb" />

          "For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras

          X Offline
          X Offline
          Xmen Real
          wrote on last edited by
          #4

          okay do this

              System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
              xdoc.XmlResolver = null;
              
              //load the xml file
              xdoc.Load(this.\_XmlFilePath);
          
              System.Xml.XmlElement Node = (System.Xml.XmlElement)xdoc.DocumentElement.SelectSingleNode("//configuration/connectionStrings/add\[@name=\\"Application.Properties.Settings.DBConnectionString\\"\]");
              if (Node != null)
              {
                  //key found, set the value
                  Node.Attributes.GetNamedItem("Source").Value = "C:\\DataDabe\\DB.mdb";
          
                  //finally, save the new version of the config file
                  xdoc.Save(this.\_XmlFilePath);
              }
          

          TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

          ----------------------------------------------- 128 bit encrypted signature, crack if you can

          J 1 Reply Last reply
          0
          • X Xmen Real

            okay do this

                System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
                xdoc.XmlResolver = null;
                
                //load the xml file
                xdoc.Load(this.\_XmlFilePath);
            
                System.Xml.XmlElement Node = (System.Xml.XmlElement)xdoc.DocumentElement.SelectSingleNode("//configuration/connectionStrings/add\[@name=\\"Application.Properties.Settings.DBConnectionString\\"\]");
                if (Node != null)
                {
                    //key found, set the value
                    Node.Attributes.GetNamedItem("Source").Value = "C:\\DataDabe\\DB.mdb";
            
                    //finally, save the new version of the config file
                    xdoc.Save(this.\_XmlFilePath);
                }
            

            TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

            ----------------------------------------------- 128 bit encrypted signature, crack if you can

            J Offline
            J Offline
            jadughar
            wrote on last edited by
            #5

            perfect thanks!!!:thumbsup::thumbsup:

            "For as long as men massacre animals, they will kill each other. Indeed, he who sows the seed of murder and pain cannot reap joy and love." Pythagoras

            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