question

fzkim-3516 avatar image
0 Votes"
fzkim-3516 asked JackJJun-MSFT commented

How can I change only part of a string?

Here is a example xml.

<group>
  <value>
    <Item>1</Item>
    <Item>2</Item>
    ...
    ...
  </value>
</group>


I only want to change betweem \<value> and <\/value>.<br/>
For example like this.

<group>
  <value>
    &lt;Item&gt;1&lt;/Item&gt;
    &lt;Item&gt;2&lt;/Item&gt;
    ...
    ...
  </value>
</group>

I'm trying with this code.

string change = File.ReadAllText(File1Path);
change = change.Replace("<", "&lt;");
change = change.Replace(">", "&gt;");
File.WriteAllText(File1Path, change);


Thank you!

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT commented

@fzkim-3516, Welcome to Microsoft Q&A, you could try the following code to change part of a string in the xml file.

Code:

  XDocument doc=XDocument.Load(path);
     var result = doc.Descendants("value").Descendants().Cast<XElement>().ToList();
     string elestr=string.Empty;
   
     for (int i = 0; i < result.Count(); i++)
     {
         StringWriter myWriter = new StringWriter();
         elestr = result[i].ToString();
         HttpUtility.HtmlEncode(elestr,myWriter);
         result[i].ReplaceWith(Environment.NewLine+myWriter.ToString()+Environment.NewLine);
     }
     doc.Save(path);
     string txt=File.ReadAllText(path);
     txt = txt.Replace("&amp;", "&");
     txt = txt.Replace("&quot;", "\"");
     File.WriteAllText(path, txt);
     var lines = File.ReadAllLines(path).Where(line => !String.IsNullOrWhiteSpace(line));
     File.WriteAllLines(path, lines);

Note: Please add namespace in your app:

 using System.IO;
 using System.Linq;
 using System.Web;
 using System.Xml.Linq;

Tested result:

206114-image.png

Hope this could help you.

Best Regards,
Jack




If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (7.8 KiB)
image.png (8.0 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JackJJun-MSFT Thank you so much! I found a problem that if I have "", it's changed to &quot. but I dont want to change. How can I solve?

0 Votes 0 ·

@fzkim-3516, thanks for the feedback, in my code example, there is no &quot in my tested result. Do you mean that you have the following xml:

 <Item name="test1">1</Item>

If not, Please tell me where you have &quot in your file.


1 Vote 1 ·

@JackJJun-MSFT Yes. I mean that kind of xml file.

0 Votes 0 ·
Show more comments