gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:23 25 4
gpt4 key购买 nike

我收到了来自客户的 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);
}

最佳答案

代替 StreamReader,使用 StringReader , 这需要 string as constructor parameter .

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

关于c# - XML Deserilation 路径错误中的非法字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com