gpt4 book ai didi

c# - bool 反序列化错误

转载 作者:行者123 更新时间:2023-11-30 22:24:01 25 4
gpt4 key购买 nike

我在对 XML 元素进行反序列化时遇到问题,我假设这与反序列化程序未找到 XML 元素中的命名空间有关。

数据来自外部来源,我无法将其修改为字符串,而且我使用的是 C# 4.0。

任何帮助,不胜感激。

string xml = "<boolean xmlns=\"http://schemas.microsoft.com/2003/10/serialization/\">false</boolean>";

var xSerializer = new XmlSerializer(typeof(bool));
using (var sr = new StringReader(xml))
using (var xr = XmlReader.Create(sr))
{
var y = xSerializer.Deserialize(xr);
}

错误:

System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=There is an error in XML document (1, 2).
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
...
...
...
InnerException: System.InvalidOperationException
HResult=-2146233079
Message=<boolean xmlns='http://schemas.microsoft.com/2003/10/serialization/'> was not expected.
Source=System.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializationPrimitiveReader.Read_boolean()
at System.Xml.Serialization.XmlSerializer.DeserializePrimitive(XmlReader xmlReader, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
InnerException:

最佳答案

如果您按如下方式创建序列化程序,它将起作用

var xSerializer = new XmlSerializer(typeof(bool),null, null, 
new XmlRootAttribute("boolean"),
"http://schemas.microsoft.com/2003/10/serialization/");

关于c# - bool 反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13014599/

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