How to read and write ¬ in Xml

2019-09-08 06:50发布

问题:

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