Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 544
Mohan Avatar asked Aug 24 '11 18:08

Mohan


2 Answers

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"))
{            
}
like image 96
alf Avatar answered Oct 19 '22 14:10

alf


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.

like image 37
Jim Wooley Avatar answered Oct 19 '22 13:10

Jim Wooley