kalls
kalls

Reputation: 2855

linq query to get value from XML to a c# class properties - Linq To XML

<Response SessionId="55C10AC0E63B44FCB1868FF4B49E6DF7" xmlns="http://MySample.Sample.com/Sample.xsd">
  <Status Success="true" Message="OK" ErrorCode="0" /> 
  <Data>
  <List Name="My.API.Customer.Customers" Type="My.API.Customer.Customers">
    <Item Key="12345678-0" State="Unchanged">
    <Value Name="MasterCustomerId" Value="12345678" /> 
     <Value Name="SubCustomerId" Value="0" /> 
    <Value Name="IsAbstractAuthor" Value="False" /> 
    <Value Name="LastFirstName" Value="Cal, Duke" /> 
    <Value Name="IsAbstractReviewer" Value="False" /> 
    <Value Name="NamePrefix" Value="" /> 
    <Value Name="FirstName" Value="Cal" /> 
    <Value Name="MiddleName" Value="" /> 
    <Value Name="LastName" Value="Duke" /> 
    <Value Name="NameSuffix" Value="" /> 
    <Value Name="NameCredentials" Value="" /> 
    <Value Name="SearchName" Value="DUKE;CAL" /> 
    <Value Name="LabelName" Value="Cal Duke" /> 
    <Value Name="FormalSalutation" Value="Mr. Duke" /> 
    <Value Name="IsCustomerStatusActive" Value="True" /> 
    <Value Name="OrganizationId" Value="" /> 
    <Value Name="OrganizationUnitId" Value="" /> 
    <Value Name="RecordType" Value="I" /> 
    <Value Name="CanPlaceOrderFlag" Value="True" /> 
    <Value Name="CanCreateSegmentsFlag" Value="False" /> 
    <Value Name="BillPrimaryAccountFlag" Value="True" /> 
    <Value Name="Nickname" Value="" /> 
    <Value Name="InformalSalutation" Value="Cal" /> 
    <Value Name="CustomerClassCode" Value="INDIV" /> 
    <Value Name="CustomerStatusCode" Value="ACTIVE" /> 
    <Value Name="CustomerStatusDate" Value="8/9/2010 4:17:23 PM" /> 
    <Value Name="AllowFaxFlag" Value="True" /> 
    <Value Name="AllowEmailFlag" Value="True" /> 
    <Value Name="AllowPhoneFlag" Value="True" /> 
    <Value Name="AllowLabelSalesFlag" Value="True" /> 
    <Value Name="AllowSolicitationFlag" Value="True" /> 
    <Value Name="AllowInternalMailFlag" Value="True" /> 
    <Value Name="SolicitationRemovalDate" Value="12:00:00 AM" /> 
    <Value Name="TaxableFlag" Value="True" /> 
    <Value Name="FederalTaxId" Value="" /> 
    <Value Name="VATId" Value="" /> 
    <Value Name="TaxExemptId" Value="" /> 
    <Value Name="Ssn" Value="" /> 
    <Value Name="GenderCode" Value="M" /> 
    <Value Name="BirthDate" Value="12:00:00 AM" /> 
    <Value Name="EthnicityCode" Value="99" /> 
    <Value Name="AnnualIncomeRangeCode" Value="" /> 
   </Item>
 </List>
    </Data>
</Response>

If I have C# class called SampleUser with properties for FirstName,LastName etc., how can I use linq-to-Xml and get the values from the XML to be assigned to the appropriate C# class properties.

Upvotes: 0

Views: 239

Answers (3)

Ani
Ani

Reputation: 113402

LINQ to XML isn't the best tool for this purpose.

If you have an XSD schema for the response, you can use the XSD.exe tool that comes with Visual Studio to generate a class for you. It will also generate a Deserialize factory method that will allow you to create an instance of the class from XML data.

If you don't have an XSD, you can use a tool that will infer the schema from a sample. Here's an example of such an app.

Upvotes: 0

Anthony Pegram
Anthony Pegram

Reputation: 126804

The descendants of Item all having the same ID sort of makes this difficult for Linq-to-XML (from a code-bloating standpoint), but not impossible.

A short example to get you started is this

XDocument document = XDocument.Parse(xml);
XNamespace ns = "http://MySample.Sample.com/Sample.xsd";

var sampleUsers = from item in document.Root.Element(ns + "Data").Element(ns + "List").Elements(ns + "Item")
                  select new SampleUser
                  {
                      FirstName = item.Elements(ns + "Value").First(v => v.Attribute("Name").Value.Equals("FirstName")).Attribute("Value").Value,
                      LastName = item.Elements(ns + "Value").First(v => v.Attribute("Name").Value.Equals("LastName")).Attribute("Value").Value
                  };

It would be far better if the XML was like

<Item>
  <FirstName>Bob</FirstName>
  <LastName>Smith</LastName>
</Item>

Perhaps you can explore a transformation to actually get there.

Upvotes: 0

Vinay B R
Vinay B R

Reputation: 8421

XDocument doc = XDocument.Parse(xmlstring);
var a = from b in doc.Descendants("Value")
        select new SampleUser(){FirstName = b.Attribute("firstName").Value,
                                lastName = b.Attribute("lastName").value};

Upvotes: 1

Related Questions