日期:2014-05-17  浏览次数:20964 次

C# XML中十六进制字符的解决方案
string filePath = "c://data.xml";
            string str = "sa,,wer";
            try
            {
                XElement root = new XElement(new XElement(str));
                root.Save(filePath);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }




上面的代码是我写的简单事例,实际中我是得到的字符串要写入xml。
实际中不能采用去除特殊字符的方法,如str1="123,123" str2="123/123" 如果去除特殊字符,那么str1和str2将都变为"123123",无法区分。想问问看谁有好的解决方案,如果转码的话,用那种比较好,最好能把解决的代码贴上来,谢谢了
C#?XML?LINQ?十六进制

------解决方案--------------------
使用CDATA,或者做一个BASE64编码。
------解决方案--------------------
对于特殊字符可以用base64编码后写入,读取时解码即可
------解决方案--------------------
你在名称中定义符号肯定是不可以的。节点名称不允许有特殊字符的。

属性值,节点值才可以。
------解决方案--------------------

        public static string XmlEncode(string s)
        {
            s = s.Replace("&", "&");
            for (int i = 0; i < 32; i++)
            {
                s = s.Replace(((char)i).ToString(), "&amp;#" + i + ";");
            }
            for (int i = 95; i < 97; i++)
            {
                s = s.Replace(((char)i).ToString(), "&amp;#" + i + ";");
            }
    &nbs