如何反序列化XML字符串到对象

时间:2018-07-10 06:18:47

标签: c# xml xmlserializer xml-deserialization

从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; }
}

1 个答案:

答案 0 :(得分:1)

一种方法是复制原始xml并转到visual studio->编辑->选择性粘贴->粘贴Xml作为类。它将为您生成类。它们的格式不会那么好,但是您可以对其进行重构。 See here