1

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 &amp; 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.

1
  • What corresponding element? You haven't given us nearly enough information about what you're trying to do. All we know is that you've got some XML and you're having problems with it. Please read tinyurl.com/so-hints Commented Mar 5, 2012 at 7:01

2 Answers 2

1

As kjn pointed out, Name and FirstName elements are not in the profile namesapce, they are in vCard. And you have to reflect that in your code. You could also simplify your code a lot:

XNamespace profileNs = "http://www.SumURL.com/XML/profile/2.0#";
XNamespace vCardNs = "http://www.w3.org/2001/vcard-rdf/3.0#";
var a = (from level in xDoc.Descendants(profileNs + "personal")
         select new
         {
             PeopleID = (string)level.Element(profileNs + "id"),
             FirstName = (string)level.Elements(vCardNs + "Name")
                                      .Elements(vCardNs + "FirstName")
                                      .FirstOrDefault()
         }).ToList();

Note that this will sometimes set the properties to null, instead of string.Empty, but I think that makes more sense if the data is not present.

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

Comments

1

I think env + "Name" will resolve as "profile:Name" and not "vCard:Name" as you would probably want.

1 Comment

The above isn't particularly correct but the vCard namespace being wrong is the main reason for the code not working.

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.