I am trying to parse the below XML in to the Entity through Linq, but i am not able to get the corresponding element.
<profile:learner type="" xmlns:profile="http://www.SumURL.com/XML/profile/2.0#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#">
<profile:personal type="">
<profile:id>123</profile:id>
<vCard:Name rdf:parseType="Resource" type="">
<vCard:FirstName>ABC</vCard:FirstName>
<vCard:LastName>XYZ</vCard:LastName>
<vCard:UserName>XYZ ABC</vCard:UserName>
<vCard:FullName>ABC XYZ</vCard:FullName>
</vCard:Name>
<vCard:Address rdf:parseType="Resource" type="">
<vCard:Street />
<vCard:Extadd />
<vCard:Locality />
<vCard:Region />
<vCard:PinCode />
</vCard:Address>
<vCard:TelephoneNumber />
<vCard:EmailId />
<vCard:TimeZone>(GMT-05:00) Eastern Time (US & Canada)</vCard:TimeZone>
<vCard:Title />
<vCard:Organization rdf:parseType="Resource" type="">
<vCard:OrgName>Q</vCard:OrgName>
</vCard:Organization>
<vCard:Role />
</profile:personal>
</profile:learner>
XNamespace env = xDoc.Root.Name.NamespaceName;
var a = (from level in xDoc.Descendants(env + "personal")
select new
{
PeopleID = (!string.IsNullOrEmpty(level.Elements(env + "id").First().Value)) ? level.Elements(env + "id").First().Value : String.Empty,
FirstName = (!string.IsNullOrEmpty(level.Elements(env + "Name").Elements(env + "FirstName").First().Value)) ? level.Elements(env + "Name").Elements(rdf + "FirstName").First().Value : String.Empty,
}).ToList();
I am able to get the PersonID but not the FirstName, Lastname, Role etc.
Please tell me where i am doing wrong in the above Linq Query.
Please help me out.
As kjn pointed out,
NameandFirstNameelements are not in theprofilenamesapce, they are invCard. And you have to reflect that in your code. You could also simplify your code a lot:Note that this will sometimes set the properties to
null, instead ofstring.Empty, but I think that makes more sense if the data is not present.