Search code examples
c#asp.netxmlxml-parsingxmldocument

Error: XmlException was unhandled by user code


I have an error like: "An exception of type 'System.Xml.XmlException' occurred in System.Xml.dll but was not handled in user code"

In my program, i use a service reference from this site: http://www.oorsprong.org/websamples.countryinfo/CountryInfoService.wso and when running the program, it stops me to the string.

The XML file: (I want to take only the sName)

<ArrayOftContinent>
    <tContinent>
        <sCode>AF</sCode>
        <sName>Africa</sName>
    </tContinent>
</ArrayOftContinent>

Here is the code:

org.oorsprong.www.CountryInfoService myWS3 = new org.oorsprong.www.CountryInfoService();
string str = Convert.ToString(myWS3.ListOfContinentsByName());
XmlDocument doc = new XmlDocument();
doc.LoadXml(str); /*It stops me here*/

Could you help me how to handle this? Thanks in advance!


Solution

  • Your wso is returning continent objects, not an xml string. Read the definition: http://www.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?op=ListOfContinentsByName

    You want to do something like this:

    org.oorsprong.www.CountryInfoService myWS3 = new org.oorsprong.www.CountryInfoService();
    var continents = myWS3.ListOfContinentsByName();
    
    foreach (var continent in continents)
           Debug.WriteLine(continent.sName)