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; }
}
3
  • Why did you manually create classes to deserialize an Atom feed into? There are existing libraries, or if you don't want to use those, generate the proper classes. These don't match the XML. Commented Jul 10, 2018 at 6:32
  • So how to match the XML? So that the data get mapped to it. Commented Jul 10, 2018 at 6:33
  • stackoverflow.com/questions/4203540/… Commented Jul 10, 2018 at 6:33

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.