Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET cannot parse xml! Check file path

I created a simple API via ASP.NET MVC 4:

public class ActionController : ApiController
{
    [WebMethod]
    public string getCommunities()
    {
        try
        {
            MethodClass method = new MethodClass();
            return method.getCommunities();
        }
        catch(Exception ex)
        {
            return ex.Message.ToString();
        }
    }
}

which is trying to call this method in the Method class:

public string getCommunities()
{
    return "bbb";
}

but for whatever reason, I get this error:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">cannot parse xml! Check file path</string>

I tried Googling for the error, but came up with nothing, has anyone seen this error before? and how do I fix it?

like image 446
user979331 Avatar asked Mar 13 '23 07:03

user979331


2 Answers

As already pointed in comments, you are looking for your bug in the wrong place. method.getCommunities() is throwing an error with message "cannot parse xml! Check file path".

Googling your error it seems to me that you are throwing a custom exception: searching for that string in your code may point you to the right place.

As a quick proof of concept I changed the standard API generated by Visual Studio Web API template.

public string Get(int id)
        {
            try
            {
                var t = 0;
                var i = 1 / t;
                return "bbb";
            }
            catch { return "ABBA"; }
        }

which exactly returns the custom error message as xml string

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">ABBA</string>
like image 139
Mauro Avatar answered Apr 07 '23 01:04

Mauro


I attempted to replicate the case you mention by creating simple ActionController.cs in ASP.Net MVC 4 template as follow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Services;

namespace MvcApiApplicationTrial1.Controllers
{
  public class ActionController : ApiController
  {
    [WebMethod]
    public string getCommunities() {
      try {
        MethodClass method = new MethodClass();
        return method.getCommunities();
      } catch (Exception ex) {
        return ex.Message.ToString();
      }
    }
  }

  public class MethodClass
  {
    public string getCommunities() {
      return "bbb";
    }
  }
}

And call it in the web browser (Chrome) with the following url:

http://localhost:56491/api/Action/getCommunities

And get the following correct result:

enter image description here

If you declare, define, and call things right, your code should have no problem at all.

So, I suggest you to re-check your declaration, definition, as well as your calling to the related Controller/Method again. Your problem may lay somewhere else.

And since the error seems to be a custom error, judging from the code posted alone, likely that the problem lays somewhere in your getCommunities method. Check the method, try to find the "cannot parse xml!" text there. Alternatively, but less likely, the error is in the MethodClass constructor. Same thing, check your MethodClass, try to find the "cannot parse xml!" text.

As for the given case as what you have posted in your question, I found no issue at all.

But anything else in between try and "bbb" can also potentially be the source of the created error. Checking the error text would be my first step if there are more things in the try block and I am unsure where the error may actually be generated.

like image 23
Ian Avatar answered Apr 07 '23 01:04

Ian