首页 > 解决方案 > 如何将 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&amp;$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; }
}

标签: c#xmlxmlserializerxml-deserialization

解决方案


一种方法是复制原始 xml 并转到 Visual Studio -> 编辑 -> 选择性粘贴 -> 将 Xml 粘贴为类。它会为你生成课程。它们的格式不会那么好,但你可以重构它。看这里


推荐阅读