0

I have a xml data like this

string data = @"<DeliveryReport><message id=""093102403501103726"" 
    sentdate=""2013/10/24 08:50:11"" donedate=""2013/10/24 08:50:12""
    status=""NOT_DELIVERED"" gsmerror=""1"" /></DeliveryReport>";

and am converting/parsing it like

if (data != null)
{
    XDocument xmlDoc = XDocument.Load(data);

    var tutorials = from tutorial in xmlDoc.Descendants("message")
        select new
        {

        };
}

is is throwing error Illegal characters in path. can any body tell me what i m misisng?

David
  • 34,223
  • 3
  • 62
  • 80
DDR
  • 1,272
  • 1
  • 12
  • 19
  • @SteveB: It's not a duplicate of that - one was using the `XmlTextReader` constructor, this is using `XDocument.Load`. Sure, it's the same idea of "you've provided text to a method accepting a filename/URI" but the best solution is somewhat different. – Jon Skeet Oct 24 '13 at 13:24
  • @SteveB: Right - that's definitely a duplicate. Although I prefer my answer here ;) – Jon Skeet Oct 24 '13 at 13:32

2 Answers2

3

You're using XDocument.Load(string), which accepts a filename or URL to load the XML from:

Parameters
uri: A URI string that references the file to load into a new XDocument.

You want XDocument.Parse(string), which accepts XML already in a string:

Parameters
text: A string that contains XML.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Try using the TextReader overload of Load when you create your XDocument:

if (data != null)
{
    XDocument xmlDoc = XDocument.Load(new System.IO.StringReader(data));

    var tutorials = from tutorial in xmlDoc.Descendants("message")
                    select new
                    {

                    };

}
Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • 1
    Why bother with that when XDocument.Parse handles it already? – Jon Skeet Oct 24 '13 at 13:18
  • @JonSkeet I wasn't aware of parse actually. I always use the `TextReader` overload for my XDocuments - mainly because they are either a) from file or b) stackoverflow answers that originally used a stream. – Gusdor Oct 24 '13 at 13:22
  • If they're from a stream, I'd use `XDocument.Load(Stream)` which allows the parser to autodetect the encoding - safer than doing that twice... – Jon Skeet Oct 24 '13 at 13:23
  • @JonSkeet Noted sir. Thanks for the input. Every day is a school day. – Gusdor Oct 24 '13 at 13:24