user2609980
user2609980

Reputation: 10484

Replace part of XML by using regex capture group

I want to capture a part of an XML string and replace a captured value with a new one.

I have the following code:

Regex regex = new Regex("<ns1:AcctId>(?<AcctId>.*?)</ns1:AcctId>");
Match match = regex.Match(Xml);

string AcctId = match.Groups["AcctId"].Value;

string IBANizedAcctId = IBANHelper.ConvertBBANToIBAN(AcctId);

newXml = Regex.Replace(oldXml, regex, IBANizedAcctId); //DOES NOT WORK!

So I want to capture the AcctId in the ns1:AcctId XML element. Then I want to replace this one with a new value by converting the BBAN to IBAN and replace the value. The first part works, but I do not know how to accomplish the last part (I did find an idea here, but I do not understand it).

I hope someone can help me out!

Upvotes: 0

Views: 435

Answers (1)

MaxOvrdrv
MaxOvrdrv

Reputation: 1916

Regex regex = new Regex("<ns1:AcctId>(?<AcctId>.*?)</ns1:AcctId>");
Match match = regex.Match(oldXml);

string AcctId = match.Groups["AcctId"].Value;

string IBANizedAcctId = IBANHelper.ConvertBBANToIBAN(AcctId);

newXml = oldXml.Replace(AcctId, IBANizedAcctId); //should work...

Upvotes: 1

Related Questions