Click here to Skip to main content
15,848,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to extract elements from XDocument and store it in class object.

I am getting empty result when I try Descendants and Elements functions, below is the structure of xml I get

XML
<getformsresponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://services.sample.com/forms">
  <eventid>00000000-0000-0000-0000-000000000000</eventid>
  <statuscode>0</statuscode>
  <statusmessage i:nil="true" />
  <forms>
    <form>
      <description>Application</description>
      <edition>2013/01</edition>
      <effectivedate>01/01/2013</effectivedate>
      <gid>00000000-0000-0000-0000-000000000000</gid>
      <linesofbusiness>
        <lineofbusiness>WORK</lineofbusiness>
      </linesofbusiness>
      <mappingenabled>true</mappingenabled>
      <name>130</name>
      <states i:nil="true" />
    </form>
    <form>
      <description>Property</description>
      <edition>2011/10</edition>
      <effectivedate>10/01/2011</effectivedate>
      <gid>00000000-0000-0000-0000-000000000000</gid>
      <linesofbusiness>
        <lineofbusiness>PROPC</lineofbusiness>
      </linesofbusiness>
      <mappingenabled>true</mappingenabled>
      <name>140</name>
      <states i:nil="true" />
    </form>
    <form>
      <description>Commercial Application</description>
      <edition>2013/09</edition>
      <effectivedate>09/01/2013</effectivedate>
      <gid>00000000-0000-0000-0000-000000000000</gid>
      <linesofbusiness>
        <lineofbusiness>AAPPL</lineofbusiness>
        <lineofbusiness>ACCT</lineofbusiness>
      </linesofbusiness>
      <mappingenabled>true</mappingenabled>
      <name>125</name>
      <states i:nil="true" />
    </form>
</getformsresponse>



I created class like below

XML
public class GetFormsListResponse
{
    public string Name { get; set; }

    public string Gid { get; set; }

    public string EffectiveDate { get; set; }

    public string Edition { get; set; }

    public string Description { get; set; }

    public StatesResponse States { get; set; }

    public LobResponse LineOfBusiness { get; set; }
}

public class GetFormsResponseCollection : List<GetFormsListResponse>
{

}

public class LobResponse : List<string>
{

}

public class StatesResponse : List<string>
{

}



and trying to put the extracted result into the collection.
Posted
Comments
Maarten Kools 29-Jan-14 16:32pm    
And what have you tried to retrieve the data..?
sam3440 29-Jan-14 16:37pm    
Loaded the xml to XDocument, and when I try to parse using doc.Descendants().Elements("Forms") it returns empty enumeration.
Maarten Kools 29-Jan-14 16:40pm    
What you're looking for is doc.Descendants("form"). This will return an IEnumerable of XElement representing each form element.
sam3440 29-Jan-14 16:47pm    
Here is wat I did and did not get any element

XDocument doc = XDocument.Parse(response);
var obj = doc.Descendants("form");

foreach (var i in obj)
{
//to put that in class objects
}

Okay, the issue here is you have specified a default namespace (missed that)

Try the following code:
C#
XDocument doc = XDocument.Parse(response);

XNamespace ns = "http://services.sample.com/forms";
IEnumerable<xelement> descendants = doc.Descendants(ns + "form"); // An alternative notation would be doc.Descendants("{http://services.sample.com/forms}forms")
foreach (XElement d in descendants)
{
    // process each form element.
}
</xelement>


In your example XML the last forms tag is missing, I assume that's not the case with the actual response?
 
Share this answer
 
C#
XmlDocument doc = new XmlDocument();
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", "http://services.sample.com/eforms");
doc.Load(@"C:\\Response.xml");
XmlNode formsNode = doc.SelectSingleNode("/x:getformsresponse/x:forms", nsmgr);
XmlNodeList formNodeList = formsNode.SelectNodes("x:form", nsmgr);

GetFormsResponse responseList = new GetFormsResponse();
foreach (XmlNode i in formNodeList)
{
  GetFormsData data = new GetFormsData();

  data.Description = i["description"].InnerText;
  data.Edition = i["edition"].InnerText;
  data.EffectiveDate = i["effectivedate"].InnerText;
  data.Gid = i["gid"].InnerText;
  data.Name = i["name"].InnerText;

  foreach (XmlNode l in i["linesofbusiness"])
  {
    LineOfBusiness buss = new LineOfBusiness();
    buss.Add(l.InnerText);
    data.LineOfBusiness = buss;
  }

  foreach (XmlNode l in i["states"])
  {
    States state = new States();
    state.Add(l.InnerText);
    data.States = state;
  }

  responseList.Add(data);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900