73

I am querying a soap based service and wish to analyze the XML returned however when I try to load the XML into an XDoc in order to query the data. am getting an 'illegal characters in path' error message? This (below) is the XML returned from the service. I simply want to get the list of competitions and put them into a List I have setup. The XML does load into an XML Document though so must be correctly formatted?.

Any advice on the best way to do this and get round the error would be greatly appreciated.

<?xml version="1.0" ?> 
- <gsmrs version="2.0" sport="soccer" lang="en" last_generated="2010-08-27 20:40:05">
- <method method_id="3" name="get_competitions">
  <parameter name="area_id" value="1" /> 
  <parameter name="authorized" value="yes" /> 
  <parameter name="lang" value="en" /> 
  </method>
  <competition competition_id="11" name="2. Bundesliga" soccertype="default" teamtype="default" display_order="20" type="club" area_id="80" last_updated="2010-08-27 19:53:14" area_name="Germany" countrycode="DEU" /> 
  </gsmrs>

Here is my code, I need to be able to query the data in an XDoc:

string theXml = myGSM.get_competitions("", "", 1, "en", "yes");
XmlDocument myDoc = new XmlDocument();
MyDoc.LoadXml(theXml);
XDocument xDoc = XDocument.Load(myDoc.InnerXml);
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Kevin
  • 1,009
  • 1
  • 11
  • 23

4 Answers4

241

You don't show your source code, however I guess what you are doing is this:

string xml = ... retrieve ...;
XmlDocument doc = new XmlDocument();
doc.Load(xml); // error thrown here

The Load method expects a file name not an XML itself. To load an actual XML, just use the LoadXml method:

... same code ...
doc.LoadXml(xml);

Similarly, using XDocument the Load(string) method expects a filename, not an actual XML. However, there's no LoadXml method, so the correct way of loading the XML from a string is like this:

string xml = ... retrieve ...;
XDocument doc;
using (StringReader s = new StringReader(xml))
{
    doc = XDocument.Load(s);
}

As a matter of fact when developing anything, it's a very good idea to pay attention to the semantics (meaning) of parameters not just their types. When the type of a parameter is a string it doesn't mean one can feed in just anything that is a string.

Also in respect to your updated question, it makes no sense to use XmlDocument and XDocument at the same time. Choose one or the another.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • 2
    Thanks for the reply. This works to load it into an XMLDocument but I am still getting the illegal characters in path if I try to load this XML into an XDoc in order to query the data. Any ideas? string theXml = myGSM.get_competitions("", "", 1, "en", "yes"); XmlDocument myDoc = new XmlDocument(); myDoc.LoadXml(theXml); XDocument xDoc = XDocument.Load(myDoc.InnerXml); – Kevin Oct 30 '10 at 09:02
  • Sorry first time using Stackoverflow so having trouble adding and formatting my responses, as you can see!!! – Kevin Oct 30 '10 at 09:03
  • Thanks for the comprehensive explanation, very useful. I am now however getting the following error when I try your suggestion as shown above, any ideas? 'System.Xml.Linq.XDocument.Load(System.IO.TextReader)' cannot be accessed with an instance reference; qualify it with a type name – Kevin Oct 30 '10 at 09:21
  • 1
    @Kevin Eh, sorry, my fault. The correct code is `doc = XDocument.Load(s)` — see update. – Ondrej Tucny Oct 30 '10 at 09:24
  • If you are using `XDocument` you can simply do `var doc = XDocument.Parse(xml)` where xml is a string containing your XML. – Dan Diplo May 24 '11 at 08:50
  • @OndrejTucny You are better than Rubber Duck Debugging today! Simple autofill left me scratching my head. – Jason Geiger Jul 13 '21 at 17:50
10

Following up on Ondrej Tucny's answer :

If you would like to use an xml string instead, you can use an XElement, and call the "parse" method. (Since for your needs, XElement and XDocument would meet your needs)

For example ;

string theXML = '... get something xml-ish...';
XElement xEle = XElement.Parse(theXML);

// do something with your XElement

The XElement's Parse method lets you pass in an XML string, while the Load method needs a file name.

Harvey Darvey
  • 706
  • 8
  • 16
3

Why not

XDocument.Parse(theXml);

I assume this will be the right solution

Swaraj
  • 383
  • 2
  • 16
2

If this is really your output it is illegal XML because of the minus characters ('-'). I suspect that you have cut and pasted this from a browser such as IE. You must show the exact XML from a text editor, not a browser.

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
  • I have copied and pasted from the XML Visualier in VS 2010, these are just to expand the XML nodes are they not? – Kevin Oct 30 '10 at 09:07
  • @Kevin It's almost impossible to debug characters after they have been through a browser display. It's possible that an illegal character has been transformed into something else. I suspect the real reason is closer to @Ondrej Tucny but it's always essential to know the exact XML. For example a space at the start of the document would be illegal though you'd probably get a different error msg – peter.murray.rust Oct 30 '10 at 09:25
  • There are many XML validators online - suggest you paste any dubious XML into them, and get error messages. http://www.xmlvalidation.com/ was the first in my Googling – peter.murray.rust Oct 30 '10 at 10:20