How to read and write ¬ in Xml

旧时模样 提交于 2019-12-12 00:54:11

问题


I am writing some registry information into a XML file. There is ¬ symbol that is getting generated as a value for a string in regkey. When I write into XML I am geting and when I try read it back, I am getting an error. Is there a way to overcome this, how can I proceed?

Error while reading is:

> System.InvalidOperationException was unhandled    
> Message="There is an error in XML document (235, 28)."

Read From file:

public diagnostics readregkey(diagnostics diagnostics, string filename)
{
    diagnostics dia = null;
    using (System.IO.StreamReader sr = 
        new System.IO.StreamReader(filename, Encoding.Unicode))
    {
        System.Xml.Serialization.XmlSerializer x = 
            new System.Xml.Serialization.XmlSerializer(typeof(diagnostics));
        dia = x.Deserialize(sr) as diagnostics;
    }
    return dia;
}

Write to file:

public static void  WriteRegKey(diagnostics diagnostics, string filename)
{
    diagnostic.regKeys.Add(key(Registry.LocalMachine, sKeyGravitas));
    diagnostic.regKeys.Add(key(Registry.CurrentUser, sKeyGravitas));
    using (System.IO.StreamWriter sw = 
        new System.IO.StreamWriter(filename,false, Encoding.Unicode))
    {
        System.Xml.Serialization.XmlSerializer x = 
            new System.Xml.Serialization.XmlSerializer(typeof(diagnostics));
        x.Serialize(sw, diagnostics);
    }
}

回答1:


You can use the WriteRaw method which allows you to write out raw markup manually. This method prevents special characters from being escaped.

You can find documentation here : http://msdn.microsoft.com/en-us/library/4zhk8s1x%28v=vs.71%29.aspx



来源:https://stackoverflow.com/questions/9631122/how-to-read-and-write-%c2%ac-in-xml

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