Darren Young
Darren Young

Reputation: 11090

c# linq to xml

I have an xml string that I wish to traverse using LINQ to XML (I have never used this, so wish to learn). However when I try to use

XDocument xDoc = XDocument.Load(adminUsersXML);
        var users = from result in xDoc.Descendants("Result")
                    select new
                    {
                        test = result.Element("USER_ID").Value
                    };

I get an error message saying illegal characters in path. reading up on it, it's because I cannot pass a standard string in this way. Is there a way to use XML LINQ qith a standard string?

Thanks.

Upvotes: 2

Views: 240

Answers (3)

Rozuur
Rozuur

Reputation: 4165

I think adminUserXML is not a file but a string containing xml, which should be parsed to convert to XDocument with XDocument.Parse(adminUserXML)

Upvotes: 1

Patrice Bernassola
Patrice Bernassola

Reputation: 14416

As said in MSDN, you must use the Parse function to create a XDocument from a string.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499730

My guess is that adminUsersXML is the XML itself rather than a path to a file containing XML. If that's the case, just use:

XDocument doc = XDocument.Parse(adminUsersXML);

Upvotes: 5

Related Questions