【发布时间】:2015-06-14 07:08:18
【问题描述】:
我正在将an encoded XML document 转换为其原始格式
string myXml = oldXml.Replace("<", "<").Replace("&", "&")
.Replace(">", ">")
.Replace(""", "\"")
.Replace("'", "'");
它工作正常。但是,如果 &quot; 在另一个 &quot; 中,我想排除它。
例子:
原始 XML
//Note the title value
<v:shape id="_x0000_i1025" title="a" title "b"> </v:shape>
编码的 XML
<v:shape id="_x0000_i1025" title="a" title "b"> </v:shape>
替换后恢复的 XML
//Note the title value
<v:shape id="_x0000_i1025" title="a" title "b"> </v:shape>
正如您所见,内部 &quot; 不应转换为 "。那么我怎样才能用正则表达式进行替换,这样它就不会替换内部&quot;
谢谢
【问题讨论】:
-
从什么编码开始?如果可以的话,我会从源头上解决问题。 (如果它已将
&amp;编码为&amp;会很好 - 事实上它没有这样做表明编码部分通常被严重破坏......) -
@JonSkeet .Net 在我输入an
xmlin anotherxmlattribute时会自动执行此操作 -
我希望它能够正确地做到这一点。请提供一个简短但完整的原因示例。
-
添加到@JonSkeet,编码后的 XML 应该是这样的:
title=&quot;a&amp;quot; title(如果是这样,你的解码器就可以工作,尽管我会使用官方实现..) -
@AlexJolig 例如
HttpUtility.HtmlDecode()