XML Special Character Handling

喜欢而已 提交于 2019-12-12 03:42:40

问题


VB / C# im attempting to parse the XML file which contains the special character,

replace & with &amp
' with &apos
< with &lt
> with &gt

the string replace did not as it replaces the special character '<' , '>' in the the tags. i tried with catch XML exceptions that didn't work when there are multiple special characters in same line, it works if only one special character in a line. the MS builtin method SecurityElement.Escape didn't work, this has to be done before the XML tags are attached.

im using this file to load a SQL table.

Any other solution to handle the XML multiple special characters?


回答1:


use System.Security.SecurityElement.Escape eg : string XmlFriendly = System.Security.SecurityElement.Escape("the bad ' string");




回答2:


Why don't you use XmlTextReader? It should handle all that escaping.




回答3:


If I understand the question correctly, you are not trying to process an XML file, you are trying to process a non-XML file which would be an XML file if the special characters were correctly escaped.

So the question is, where does this non-XML file come from? Because the best way to fix it is to fix the process that created it. Creating bad XML and then repairing it is much more difficult than creating good XML in the first place.



来源:https://stackoverflow.com/questions/11229524/xml-special-character-handling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!