c# - 如何将 XML 字符串反序列化为对象
问题描述
从 Web 服务中,我得到了一个 XML 字符串,我需要将它映射到一个对象上。我正在尝试将下面的 XML 映射到一个对象,但条目为空。
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<feed xml:base=\"http://abc.example.com/pwa/_api/ProjectData/\" xmlns=\"http://www.w3.org/2005/Atom\" xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\" xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">
<id>http://abc.example.com/pwa/_api/ProjectData/Projects</id>
<title type=\"text\">Projects</title>
<updated>2018-07-10T06:06:50Z</updated>
<link rel=\"self\" title=\"Projects\" href=\"Projects\" />
<entry>
<id>http://abc.example.com/pwa/_api/ProjectData/Projects(guid'e396cf5d-43c1-e611-941d-00155d064605')</id>
<category term=\"ReportingData.Project\" scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\" />
<link rel=\"edit\" title=\"Project\" href=\"Projects(guid'e396cf5d-43c1-e611-941d-00155d064605')\" />
<title />
<updated>2018-07-10T06:06:50Z</updated>
<author>
<name />
</author>
<content type=\"application/xml\">
<m:properties>
<d:ProjectName>PROJ - 01</d:ProjectName>
</m:properties>
</content>
</entry>
<entry>
<id>http://abc.example.com/pwa/_api/ProjectData/Projects(guid'7d931b63-cd80-e711-941f-00155d064605')</id>
<category term=\"ReportingData.Project\" scheme=\"http://schemas.microsoft.com/ado/2007/08/dataservices/scheme\" />
<link rel=\"edit\" title=\"Project\" href=\"Projects(guid'7d931b63-cd80-e711-941f-00155d064605')\" />
<title />
<updated>2018-07-10T06:06:50Z</updated>
<author>
<name />
</author>
<content type=\"application/xml\">
<m:properties>
<d:ProjectName>PROJ - 02</d:ProjectName>
</m:properties>
</content>
</entry>
<link rel=\"next\" href=\"http://abc.example.com/pwa/_api/ProjectData/Projects?$select=ProjectName&$skiptoken=guid'b80c2f61-2981-4a42-8f3e-9301b3871494'\" />
</feed>
这是我调用服务并读取响应的方式:
var credentials = new NetworkCredential(usr, pwd);
var request = (HttpWebRequest)WebRequest.Create("......");
request.Credentials = credentials;
var res = request.GetResponse();
var stream = res.GetResponseStream();
var reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("utf-8"), true);
string strResponse = reader.ReadToEnd();
var strReader = new StringReader(strResponse);
var serializer = new XmlSerializer(typeof(EPMProjects));
var xmlReader = new XmlTextReader(strReader);
var obj = serializer.Deserialize(xmlReader);
以及要映射的模型类:
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class EPMProjects
{
[XmlElement("entry")]
public List<EPMProject> Projects { get; set; }
public EPMProjects()
{
Projects = new List<EPMProject>();
}
}
public class EPMProject
{
[XmlElement("ProjectName")]
public string ProjectName { get; set; }
}
解决方案
推荐阅读
- python - Python Calculator 结果未显示
- python - python递归函数自动倒带
- powershell - 从 64 位进程获取来自 SYSWOW64 的文件列表
- java - 如何在 Spring MVC 中创建简单的登录 Rest API?
- amazon-web-services - AWS LogInsights 查询统计平均计数
- sql - 使用 Dapper 在 Postgres 12 上创建角色的 SQL 语句失败
- java - 使用 Spy (junit5) 模拟 jdbcTemplate 查询异常
- python - 从python中的列中提取单词匹配
- oracle - OracleNetsuite - 增值税标签显示错误的税码
- javascript - 反应原生:有办法修复反应选项卡导航 5 中的错误“动画事件必须有事件数据”吗?