XML Deserilation中路径错误中的非法字符

时间:2014-10-28 11:07:32

标签: c# xml

我的客户已收到XML回复。我无法将XML反序列化为字符串,它会引发Illegal characters in path错误。所以现在我将文件保存在临时文件夹中并检索它。是否可以在不保存XML文件的情况下进行反序列化?

string xml = Post();

XmlSerializer deserializer = new XmlSerializer(typeof(Envelope));
TextReader reader = new StreamReader(xml); <-- Illegal characters in path error -->
object obj = deserializer.Deserialize(reader);
Envelope XmlData = (Envelope)obj;
reader.Close();

修改1 -

 XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            using (TextWriter writer = new StreamWriter(xml)) <-- StringWriter is Possible here? -->
            {
                serializer.Serialize(writer, XmlData);
            } 

1 个答案:

答案 0 :(得分:5)

使用StringReader取代StreamReader,而不是TextReader reader = new StringReader(xml);

string output;
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (TextWriter writer = new StreamWriter(xml)) <-- StringWriter is Possible here? -->
{
    serializer.Serialize(writer, XmlData);

    output = writer.ToString();
} 

写作时,请使用:

{{1}}