是否可以创建一个/两个衬垫正则表达式来执行以下操作?
我想找到这个json数据:
string ourdata = @"{
'Field1':'Some ""awesome"" One',
'Field2':'second field',
'Field3':'/path/to/image.jpg',
'Field4':'f4data',
'Field5':'f5data',
'Field6':'f6data',
'Field7':'f7data',
'Field8':'f8data',
'Field9':'f9data',
'Field10':'f10data',
'Field11':'f11data',
'Field12':'f12data',
'AnotherField':'some field'
}";
在此范围内,并替换|| FieldX ||使用相应的json数据:
string fakeDoc = @"
<?xml version=""1.0"" encoding=""UTF-8"" standalone=""no""?>
<svg xmlns:xlink=""http://www.w3.org/1999/xlink"" background=""#ffffff"" xmlns:svg=""http://www.w3.org/2000/svg"" xmlns=""http://www.w3.org/2000/svg"" version=""1.2"" width=""1758"" height=""1292"" viewBox=""0 0 1758 1292"" id=""SVGCanvas"">
<flowRoot id=""return_address_root"" font-size=""36px"" font-family=""Arial"" fill=""#000000"">
<flowRegion id=""return_address_region"">
<rect width=""600"" height=""150"" x=""64"" y=""65"" fill=""none"" id=""return_address_shape""/>
</flowRegion>
<flowDiv id=""return_address""><flowPara xml:space=""preserve""><![CDATA[||Field1||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field2||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field3||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field5||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field1||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field2||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field4||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field5||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field6||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field7||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field8||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||Field9||]]></flowPara>
<flowPara xml:space=""preserve""><![CDATA[||AnotherField||]]></flowPara>
</flowDiv>
</flowRoot>
</svg>";
有可能吗?
--- --- UPDATE
这就是我现在所拥有的:
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(ourdata);
StringBuilder sb = new StringBuilder(fakeDoc);
foreach (KeyValuePair<string, string> pair in values)
sb.Replace(pair.Key, pair.Value);
Console.WriteLine(sb.ToString());
答案 0 :(得分:0)
好的,所以你显然已经将你的JSON解析成字典了。我没意见。在正则表达式之后,将从XML字符串中将变量名称作为命名捕获组:
@"\|\|(?<variableName>[^|]+)\|\|"
此表达式可用于替换所有相应变量的循环。