开发者

Double escape XML File

I have a simple xml file like this one:

<xml>
  <check>
    <a>b&lt;</a>
    <b>
      <test>asdf</test>
    </b>
    <test>jj&amp;j</test>
    </check>
</xml>

I would like to "double escape" all special chars so it will result in &amp;lt; and &amp;amp; with a simple linq to xml statement. The xml should still be valid, only the values need to be escaped. Anyone knows a solution for this?

The resulting xml should look like this:

<xml>
  <check>
    <a>b&amp;lt;</a>
    <b>
      <test>asdf</test>
    </b>
    <test>jj&amp;amp;j</test>
    </check>
<开发者_开发问答;/xml>

Thanks in advance


If you know that you only have encoded entities in the text nodes, you could simply replace every instance of & with &amp;.

Though why you would even need to do such a thing escapes me.


You can't have & in the name of an element, attribute, or declaration so the only possible place for it is in the values (text nodes and attribute values). Since this is precisely where you say you want to do this replace, then a simple blind replace is the way to go. Whether you should use string.Replace() or a streaming method depends on usage and the size of the entire body.

I agree with everyone else, this smells bad.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜