如何在Xml中读写¬

时间:2012-03-09 08:42:43

标签: c# xml visual-studio registrykey

我正在将一些注册表信息写入XML文件。 有一个符号作为regkey中字符串的值生成。 当我写入XML时,我正在考虑?当我尝试阅读它时,我收到了一个错误。有没有办法克服这个问题,我该怎么办?

阅读时出错:

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

从文件中读取:

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;
}

写入文件:

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 个答案:

答案 0 :(得分:4)

您可以使用WriteRaw方法,该方法允许您手动写出原始标记。此方法可防止转义特殊字符。

您可以在此处找到文档:http://msdn.microsoft.com/en-us/library/4zhk8s1x%28v=vs.71%29.aspx