0
<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.

1
  • maybe xmlSerializer could handle that Commented Sep 17, 2010 at 19:49

3 Answers 3

1
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};
Sign up to request clarification or add additional context in comments.

1 Comment

I had a method which returns IEnumerable<SampleUser> and tried your code Vinay It is null for a some reason. I will try it and keep you posted.
0

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.

1 Comment

That's one complex Linq you have mentioned Anthony. Thanks it works great!
0

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.

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.