1

From a web service I'm getting an XML string, which I need to map on an object. I am trying to map below XML to an object but the entries are null.

<?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>

Here's how I'm calling the service and reading the response:

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);

And the model class to get mapped on:

[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; }
}
Ali Shahzad
  • 5,163
  • 7
  • 36
  • 64

1 Answers1

2

One way to do it is just copy the original xml and go to visual studio -> Edit -> Paste Special -> Paste Xml as classes. And it will generate the classes for you. They won't be so nicely formatted, but you can refactor it. See here