gpt4 book ai didi

c# - 无法将 xml 反序列化为 List

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:51 24 4
gpt4 key购买 nike

我正在尝试使用 XmlSerializer 将 wcf Web 服务的此 xml 响应反序列化为列表,但失败并显示异常消息:XML 文档 (1、2) 中存在错误

xml:

<ArrayOfNote xmlns="http://schemas.datacontract.org/2004/07/NotebookService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Note>
<Author>Redouane</Author>
<Body>Test note</Body>
<CreationDate>2014-01-28T00:00:00</CreationDate>
<Id>1</Id>
<Title>Hello World</Title>
</Note>
</ArrayOfNote>

c#:

public class Note
{
public int Id { get; set; }
public string Title { get; set; }
public System.DateTime CreationDate { get; set; }
public string Author { get; set; }
public string Body { get; set; }
}

这是我为反序列化接收到的流而编写的代码

private HttpClient client = new HttpClient();
private List<Note> notes = new List<Note>();

...

XmlSerializer serializer = new XmlSerializer(typeof(List<Note>));
var responseData = await response.Content.ReadAsStreamAsync();
List<Note> list = serializer.Deserialize(responseData) as List<Note>;

求助!

最佳答案

如下更改序列化程序的创建,它应该可以工作。

XmlSerializer serializer = new XmlSerializer(typeof(List<Note>), 
new XmlRootAttribute("ArrayOfNote") {
Namespace = "http://schemas.datacontract.org/2004/07/NotebookService"
});

关于c# - 无法将 xml 反序列化为 List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21486313/

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