Invalid XML characters in the path

I am accessing a soap-based service and want to parse the resulting XML, however, when I try to load the XML into XDoc to request data. Am I getting an "illegal characters in transit" error message? This (below) is the XML returned by the service. I just want to get a list of contests and put them in the list that I have installed. Does XML really load into an XML document, but needs to be formatted correctly?

Any advice on the best way to do this and get around 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 request data in XDoc:

 string theXml = myGSM.get_competitions("", "", 1, "en", "yes"); XmlDocument myDoc = new XmlDocument(); MyDoc.LoadXml(theXml); XDocument xDoc = XDocument.Load(myDoc.InnerXml); 
+48
c # xml
Oct 30 '10 at 8:45
source share
4 answers

You are not showing your source code, but I assume you are doing this:

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

The Load method assumes that the file name is not XML itself. To load the actual XML, simply use the LoadXml method:

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

Similarly, using XDocument , the Load(string) method expects a file name, not the actual XML. However, there is no LoadXml method, so the correct way to load XML from a string is as follows:

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

In fact, when developing something, it is a very good idea to pay attention to the semantics (value) of parameters of not only their types. If the type of the parameter is string , this does not mean that you can only feed anything that is a string.

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

+171
Oct 30
source share

In response to Ondrej Tucny's answer:

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

For example;

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

The XElement Parse method allows you to pass an XML string, and the Load method a file name.

+8
Apr 02 '13 at 4:32
source share

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

+2
Oct 30 '10 at 8:52
source share

Why not

 XDocument.Parse(theXml); 

I guess this will be the right decision

+1
Aug 07 '14 at 8:17
source share



All Articles