How can I update my xml file (existing) data using c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • patilrajesh81
    New Member
    • Jan 2013
    • 1

    How can I update my xml file (existing) data using c#

    my file is like:
    Code:
    <?xml version="1.0" encoding="utf-8"?> <CarList 
        xmlns="http://tempuri.org"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://tempuri.org carSchema.xsd"
     > 
    <cars car_Id="1"> 
    <car_make>MARUTI</car_make> 
    <car_model>WAGONR</car_model> 
    <car_year>2000</car_year> 
    <car_price>400000</car_price> 
    <reg_num>MA123</reg_num> 
    <prev_owner>SAMEER</prev_owner> 
    </cars> 
    <cars car_Id="2" > 
    <car_make>MARUTI</car_make>
     <car_model>WAGONR</car_model> 
    <car_year>2000</car_year> 
    <car_price>400000</car_price> 
    <reg_num>MA123</reg_num> 
    <prev_owner>SAMEER</prev_owner> 
    </cars>  </CarList>
    How i can update my xml file(existing) data using c#
    Last edited by acoder; Jan 14 '13, 08:25 AM. Reason: Please use [code] tags when posting code
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    Read the whole file.
    load it into Dictionary, Class or whatever.
    Modify as need.
    Rewrite the whole file.

    Comment

    • leecorp
      New Member
      • Jan 2013
      • 10

      #3
      This code can Access the XML File

      Then Add some new Elements to it

      Then Append the XML File


      Code:
      string fileName = RemoveSpecialCharacters(DateTime.Now.ToString()) + FileUpload1.FileName;
              XmlWriterSettings st = new XmlWriterSettings();
              st.Indent = true; ;
      
              st.OmitXmlDeclaration = true;
              st.Encoding = Encoding.ASCII;
             
              
              // to append to the existing files 
              try
              {
                  XElement xml = XElement.Load(Server.MapPath(".\\XML\\data.xml"));
                  xml.Add(new XElement("Software", new XAttribute("Name",TextBox1.Text) ));
                  xml.Save(Server.MapPath(".\\XML\\data.xml"));
      
              }
              catch(Exception ex){}


      Hope this Works
      Good Day
      Last edited by Meetee; Jan 15 '13, 06:00 AM. Reason: use code tags <code/> around code

      Comment

      Working...