Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that writes to a xml file and then reads it into some text boxes.I want to be able to change the text in the textboxes and then be able to update the xml file to what the textboxes say now.How do i delete the text in the xml file and replace it with the textbox text??
here is the code i am trying
 if (isChanged)
{
      XDocument doc = XDocument.Load("SavedData.xml");
      var decendants = doc.Descendants().Where(s => s.Name ==
      tb.Name).ToList();
      foreach (XElement kid in decendants)
      {
          string value = kid.Value;
          string re= value.Replace(kid.Value, tb.Text);
      }

}


What I have tried:

i tried using the replacewith() property but it replaced everything instead of just the former text.
Posted
Updated 8-Aug-18 4:28am
v2
Comments
F-ES Sitecore 8-Aug-18 9:00am    
Load the XML into something like XmlDocument, find the relevant node and update the value for that node. If you google "c# xmldocument examples" or "c# xmldocument selectnode examples" you'll find sample code for loading xml, finding nodes etc.
Codingnow20 8-Aug-18 9:12am    
I just added the code i have been trying to get the xml text and replace it

1 solution

An XML file is a text file, with a layer of "structure" imposed upon it by the XML specification. You can't just "replace text" beicase text files don't work that way - there is no "insert" or "delete" function for a part of a text file, the only changes you can make is to append to the end, or rewrite the whole file.

So F-ES Sitecore is spot on: the best way to do this task is to read the XML file into memory using an appropriate reader (and there are quite a few of them), make your changes in memory, and then write the whole XML data back to a file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900