45

I have very simple XML in a string that I'm trying to load via XDocument so that I can use LINQ to XML:

 var xmlString = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
 <person>Test Person</person>";

 var doc = XDocument.Load(xmlString); //'Illegal characters in path' error thrown here

I get an Illegal characters in path. error thrown when I try to load the XML; could someone please explain why this is happening? Thanks.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
BoundForGlory
  • 4,114
  • 15
  • 54
  • 81

3 Answers3

137

You are looking for XDocument.Parse - XDocument.Load is for files not xml strings:

var doc = XDocument.Parse(xmlString); 
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
11

Use

var doc = XDocument.Parse(xmlString); 
Cinchoo
  • 6,088
  • 2
  • 19
  • 34
0

Use this for XML String

        XDocument reader;
        using (StringReader s = new StringReader(**XmlResult**))
        {
            reader = XDocument.Load(s);
        }
Akshay Mishra
  • 1,535
  • 2
  • 15
  • 14