简体   繁体   English

使用正则表达式捕获组替换部分XML

[英]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. 我想捕获XML字符串的一部分,并将捕获的值替换为新的值。

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. 所以我想在ns1:AcctId XML元素中捕获AcctId。 Then I want to replace this one with a new value by converting the BBAN to IBAN and replace the value. 然后,我想通过将BBAN转换为IBAN并将其替换为新值来替换它。 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! 我希望有人能帮助我!

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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM