Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Illegal Characters in path

Tags:

c#

xml

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); 
like image 803
Kevin Avatar asked Oct 30 '10 08:10

Kevin


People also ask

Are XML characters illegal?

The only illegal characters are & , < and > (as well as " or ' in attributes, depending on which character is used to delimit the attribute value: attr="must use &quot; here, ' is allowed" and attr='must use &apos; here, " is allowed' ). They're escaped using XML entities, in this case you want &amp; for & .

What does illegal characters in path mean?

The "Illegal characters" exception means that the file path string you are passing to ReadXml is wrong: it is not a valid path. It may contain '?' , or ':' in the wrong place, or '*' for example. You need to look at the value, check what it is, and work out where the illegal character(s) are coming from.

What is XML text reader?

XmlTextReader provides forward-only, read-only access to a stream of XML data. The current node refers to the node on which the reader is positioned. The reader is advanced using any of the read methods and properties reflect the value of the current node.


1 Answers

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.

like image 84
Ondrej Tucny Avatar answered Sep 23 '22 21:09

Ondrej Tucny