1

I am trying to work on a regular expression to replace XML content in C# with no success.

Here is the sample code with XML example.

     static void Main(string[] args)
    {
        Console.Write(ReplaceValue("<test val='123'><this>something</this></test>", "ANY_XML_BLOB", @"<test[^>]*>\s*(?'value'[^</test]*)"));
    }

    static string ReplaceValue(string request, string newFieldValue, string pat)
    {
        string value = String.Empty;
        Regex conditionRex = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.Compiled);
        Match match = conditionRex.Match(request);
        if(match.Success)
        {
           value = match.Groups["value"].Value;
           return request.Replace(value, newFieldValue);
        }
        return request;
    }
}

Expected output is "this" tag and all sub-contents be replaced by the word ANY_XML_BLOB.

Any help fixing this would be appreciated.

Thanks!

4
  • This is a massive duplicate question, and the answer is, you can't use a regex on XML because XML is not a "regular language". Use LINQ to XML instead. Commented Apr 23, 2011 at 1:43
  • I believe that your "can't" is more on philosophical basis. LINQ is out of question for me. Sadly, its REGEX or Die! Commented Apr 23, 2011 at 1:46
  • 1
    What about plain old DOM? Commented Apr 23, 2011 at 1:49
  • Actually, "can't" is more on a mathematical / Computer Science basis, not philosophy. Commented Apr 24, 2011 at 2:47

2 Answers 2

2

I would recommend using a proper XML parser for trying to do what you want to do. Using a regex is just asking for trouble. Something in the System.Xml namespace would suit you. You might even give LINQ to XML a try.

PsuedoCode:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("<test val='123'><this>something</this></test>");

XmlNodeList testelelist = xmlDoc.GetElementsByTagName("test");
XmlNode testele = testelelist.Item(0);
testele.InnerText = "ANY_XML_BLOB";
Sign up to request clarification or add additional context in comments.

Comments

2

While I would recommend following the XML parsing route, you COULD try this:

string output = Regex.Replace(input, "<this>.*?</this>", "ANY_XML_BLOB");

1 Comment

This works, but sadly the sample is code is what I have to use to replace the XML content. Hence, I have been using 'value' in the REGEX to replace it later, after the match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.