0

I am trying to replace content of data tag in each file, but getting error "System.ArgumentException: 'String cannot be of zero length. Parameter name: oldValue'" (*"Data" tag contains multiple lines of base64 encoded string)

Here is my code:

var regex = new Regex("<data>([\\s\\S]*?)<\\/data>\r\n", RegexOptions.Multiline);
var main1 = Directory.GetFiles(main1path, "demo.xml", SearchOption.AllDirectories).OrderBy(x => x).ToArray();
var main2 = Directory.GetFiles(main2path, "demo.xml", SearchOption.AllDirectories).OrderBy(x => x).ToArray();
for (var i = 0; i < Math.Min(main1.Length, main2.Length); i++)
{
    var demo1path = main1[i];
    var demo2path = main2[i];
    var demo1 = File.ReadAllText(demo1path);
    var demo2 = File.ReadAllText(demo2path);
    var data1 = regex.Match(demo1);
    var data2 = regex.Match(demo2);
    File.WriteAllText(demo1path, demo1.Replace(data1.Value, data2.Value));
}
9
  • Which exact line does the error come from? Commented Sep 17, 2019 at 17:13
  • 3
    data1.Value must be an empty string, meaning that your regex didn't find a match (you should see that data1.Success is false). Commented Sep 17, 2019 at 17:17
  • 6
    Also, please don't use regex to parse XML. Use an actual XML parser. Commented Sep 17, 2019 at 17:17
  • 2
    stackoverflow.com/questions/55828/how-does-one-parse-xml-files. Please Google search "How to parse xml with c#" if you need more. Commented Sep 17, 2019 at 17:28
  • 2
    Related: Why is it such a bad idea to parse XML with regex? Commented Sep 17, 2019 at 17:38

0

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.