gpt4 book ai didi

c# - XML序列化和反序列化与内存流

转载 作者:数据小太阳 更新时间:2023-10-29 02:00:44 27 4
gpt4 key购买 nike

<分区>

以下代码在尝试反序列化代码时找不到根元素时出现错误:

An exception of type 'System.InvalidOperationException' occurred in System.Xml.dll but was not handled in user code
Additional information: There is an error in XML document (0, 0).
Inner exception: {"Root element is missing."}

代码看起来很简单,但是在这个问题上谷歌搜索和搜索 SO 并没有得到任何明确的答案——只有类似的问题,但答案无济于事……或者我误解了什么。

    [TestMethod]
public void TestSerialize()
{
XmlSerializer serializer = new XmlSerializer(testObject.GetType());
MemoryStream memStream = new MemoryStream();
serializer.Serialize(memStream, testObject);

XmlSerializer xmlSerializer = new XmlSerializer(testObject.GetType());
TestObject testObj = ((TestObject)xmlSerializer.Deserialize(memStream));
assert(testObject == testObj);
}

public class TestObject
{
public int IntProp { get; set; }
public string StringProp { get; set; }
}

Root element is missing 处的所谓重复问题使用 XMLDocument 对象并有不同的正确答案。

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