gpt4 book ai didi

c# - XML 文档中的错误 (2,2)

转载 作者:太空狗 更新时间:2023-10-29 20:55:07 29 4
gpt4 key购买 nike

我有一些 xml 文件,我正尝试在给定代码中按以下方式反序列化。

using (StreamReader srFileContent = new StreamReader(filePath))  
{
XmlSerializer serializer = new XmlSerializer(typeof(messageType));
messageType objMessage = (messageType)serializer.Deserialize(srFileContent);
}

这里的file locate at filePath不包含以下几行

<?xml version="1.0"?>
<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">

这就是我收到错误的原因。你能帮我在反序列化给定文件的流之前如何添加这行运行时吗?

错误如下:

System.InvalidOperationException: There is an error in XML document (2, 2). ---> System.InvalidOperationException: was not expected. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReadermessageType.‌​Read161_message() --- End of inner exception stack trace --- at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) at CCR2BB.frmMain.BWConvertProcess_DoWork()

最佳答案

您必须查看基本异常才能找出问题所在。捕获的异常可能包含 4 个或更多内部异常。

如:

try
{
...
}
catch (Exception ex)
{
Console.WriteLine(ex.GetBaseException());
}

关于c# - XML 文档中的错误 (2,2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10737892/

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