最好的方式来连接code文本数据的XML [英] Best way to encode text data for XML

查看:176
本文介绍了最好的方式来连接code文本数据的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找的.Net为en codeA字符串中的XML元素或属性使用一个通用的方法,并且很惊讶,当我没有马上找到。所以,在我去得更远,可能我只是缺少内置的功能?

假设一下,如果它确实不存在,我把我一起自己的泛型恩codeForXml(字符串数据)办法了,我正考虑要做到这一点的最好办法。

我使用的这些数据促使这件事可以包含坏字符,如与放大器中,<,"等它也可能包含有关场合正确转义实体:&放大器;放大器;,&放大器; LT; ,并与放大器; QUOT,这意味着只用一个CDATA部分可能不是最好的主意。这似乎有点klunky anyay;我宁愿结束了,可以直接在XML中使用一个很好的字符串值。

我用一个普通的前pression在过去正好赶上不好的&符号,而我想用它来赶上他们在这种情况下,以及作为第一步,然后做一个简单的替换对于其他字符。

所以,这可能进一步优化,而不使它太复杂了,有什么我失踪?

功能连接codeForXml(BYVAL数据作为字符串)作为字符串     静态badAmpersand随着新的Regex(与&([α-ZA-Z] {2,6}; |?!#[0-9] {2,4}))     数据= badAmpersand.Replace(数据,&放大器;放大器;)     返回data.Replace(&所述;,&安培;其中;)替换(,&安培; QUOT;)更换。(>中,gt;中) 端功能

对不起,你的C# - 只folks--我真的不关心我使用的语言,但我想使正则表达式静态的,你不能这样做,在C#中未声明的方法外,所以这将是VB.Net

最后,我们还是在NET 2.0,我的工作,但如果有人可以采取的最终产品,并把它转化为String类的扩展方法,这会是pretty的冷却过。

更新最初的几个答复表明,净确实内置了这样的方法。但是现在,我已经开始了,我有种想完成我的恩codeForXml()方法只是为了好玩,所以我仍然在寻找改进意见。值得注意的是:字符应为连接$ C $光盘作为实体(可能存储在列表/地图),和东西变得比对串行不可改变串做.Replace()更好性能的更完整的列表

解决方案

了System.XML为您处理的编码,所以你并不需要这样的方法。

I was looking for a generic method in .Net to encode a string for use in an Xml element or attribute, and was surprised when I didn't immediately find one. So, before I go too much further, could I just be missing the built-in function?

Assuming for a moment that it really doesn't exist, I'm putting together my own generic EncodeForXml(string data) method, and I'm thinking about the best way to do this.

The data I'm using that prompted this whole thing could contain bad characters like &, <, ", etc. It could also contains on occasion the properly escaped entities: &amp;, &lt;, and &quot;, which means just using a CDATA section may not be the best idea. That seems kinda klunky anyay; I'd much rather end up with a nice string value that can be used directly in the xml.

I've used a regular expression in the past to just catch bad ampersands, and I'm thinking of using it to catch them in this case as well as the first step, and then doing a simple replace for other characters.

So, could this be optimized further without making it too complex, and is there anything I'm missing? :

Function EncodeForXml(ByVal data As String) As String
    Static badAmpersand As new Regex("&(?![a-zA-Z]{2,6};|#[0-9]{2,4};)")

    data = badAmpersand.Replace(data, "&amp;")

    return data.Replace("<", "&lt;").Replace("""", "&quot;").Replace(">", "gt;")
End Function

Sorry for all you C# -only folks-- I don't really care which language I use, but I wanted to make the Regex static and you can't do that in C# without declaring it outside the method, so this will be VB.Net

Finally, we're still on .Net 2.0 where I work, but if someone could take the final product and turn it into an extension method for the string class, that'd be pretty cool too.

Update The first few responses indicate that .Net does indeed have built-in ways of doing this. But now that I've started, I kind of want to finish my EncodeForXml() method just for the fun of it, so I'm still looking for ideas for improvement. Notably: a more complete list of characters that should be encoded as entities (perhaps stored in a list/map), and something that gets better performance than doing a .Replace() on immutable strings in serial.

解决方案

System.XML handles the encoding for you, so you don't need a method like this.

这篇关于最好的方式来连接code文本数据的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆