Mohan Kumar
Mohan Kumar

Reputation: 6056

reading an XML string using LINQ

I am calling a sharepoint service /_vti_bin/usergroup.asmx from my silverlight app. In that the method GetAllUserCollectionFromWeb() returns the XML string. I need to iterate through that XML string to get the required data. But the LINQ to XML in this scenario is not working, as it is working when loading the XML file and getting the req data. How to do the similar functionality of LINQ to SQL with an XML string?

Sample code:

string str = @"<LanguageDetails>
                        <UserNode>
                            <Lang>
                                English
                            </Lang>
                        </UserNode>
                    </LanguageDetails>";

Need to handle the similar string and iterate to read the value using LINQ to XML.

Upvotes: 5

Views: 11854

Answers (2)

Jim Wooley
Jim Wooley

Reputation: 10398

In almost all cases where you return no rows when doing LINQ to XML queries, the reason is because there is a namespace in your XML. Check the root nodes to see if there are any namespaces and include them in your LINQ queries.

Upvotes: 1

alf
alf

Reputation: 18530

You mean something like this?

string str = @"<LanguageDetails>
                   <UserNode>
                       <Lang>
                           English
                       </Lang>
                   </UserNode>
               </LanguageDetails>";
XElement xLanguageDetails = XElement.Parse(str);
foreach (XElement xUserNode in xLanguageDetails.Elements("UserNode"))
{            
}

Upvotes: 6

Related Questions