Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to delete the output between the value tags but am falling to do so. i only want to delete the value tag that is within the data which is my table. can you please help me delete those values. keep it in mind that those output are not hard-coded,the user just entered those values, so every time i load the form i want to delete those output between the tags.

XML
<data name="CurrentSelectedUser_Label" xml:space="preserve">
    <value>Current selected record:</value>
    <comment>[Font]Bold[/Font][DateStamp]2015/01/01 00:00:00[/DateStamp][Comment] this is a comment.!![/Comment]</comment>
  </data>
  <data name="FinEnrolment_Exit_Button" xml:space="preserve">
    <value>Exit Finger Enrolment</value>
    <comment>[Font]Regular[/Font][DateStamp]2014/012/01 00:00:00[/DateStamp][Comment] this is a comment.!![/Comment]</comment>
  </data>


i tried this, but am failing to specify the value tags that are within the data which is my table. see my coding below;

VB
string text = File.ReadAllText(outputFilePath);
                text = DeleteBetween1(text, "<value>", "</value>");


C#
public string DeleteBetween1(string STR, string FirstString, string LastString)
       {
           string regularExpressionPattern1 = "<value(.*?)>";
           Regex regex = new Regex(regularExpressionPattern1,            RegexOptions.Singleline);
           MatchCollection collection = regex.Matches(STR.ToString());
           var val = string.Empty;
           foreach (Match m in collection)
           {
               val = m.Groups[1].Value;
               STR = STR.Replace(val, "");
           }
           STR = STR.Replace(val, "");
           return STR;
       }
Posted
Updated 16-Feb-15 22:45pm
v3

Good day,

If I understood correctly, you want to remove the "<value></value>" tag from within the text.

Using your Function DeleteBetween1(...), you will need to change the Regular Expression pattern to include both tags and whatever is in between.

string regularExpressionPattern1 = "<value>.*</value>";

That should look for any text matching your criteria

You would also need to change your RegexOptions to either None, or Multiline
Regex regex = new Regex(regularExpressionPattern1, RegexOptions.None | RegexOptions.Multiline);

That should find all occurrences of your criteria in the text.

For the replacement part you only need to execute this inside the loop for each Match
STR = STR.Replace(m.Value, "");


Hope it solves your issue.

Complete Code :
public static string DeleteBetween1(string STR, string FirstString, string LastString)
{
    string regularExpressionPattern1 = "<value>.*</value>";
    Regex regex = new Regex(regularExpressionPattern1, RegexOptions.None | RegexOptions.Multiline);
    MatchCollection collection = regex.Matches(STR.ToString());
    foreach (Match m in collection)
    {
        STR = STR.Replace(m.Value, "");
    }
    return STR;
}
 
Share this answer
 
Comments
Bacanzela 17-Feb-15 5:48am    
Your code works but not meeting my needs, i want it delete what is between these tags <value><value>. this tags are all over the forms but i only want to delete within the "data" which is the name of my table. from your code it doesn't delete from the file,it only clears on load. i want it to delete from the file as well.
moh9696 17-Feb-15 6:52am    
The function will return a string that you can save back to the file opened.
File class has function WriteAllText(path, content).
provide it with the same path used to load the file, "outputFilePath" in your case, and it should write back the new content without the value tags to that file.
I hope this helps.

Cheers,
Hi,

You can use one method to delete the value tag.
Follow the below line of code which can help you.

XML
string xx = @"<data name=""CurrentSelectedUser_Label"" xml:space=""preserve"">
                                <value>Current selected record:</value>
                                <comment>[Font]Bold[/Font][DateStamp]2015/01/01 00:00:00[/DateStamp][Comment] this is a comment.!![/Comment]</comment>
                          </data>";
            var xd = XElement.Parse(xx);
            Response.Write(xx.Replace(xd.Element("value").ToString(),""));


Here I have used the String value as input. Based on your requirement you can change.

Thanks
Sisir Patro
 
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