XmlException: Root element is missing in ASP .NET Core

2.3k views Asked by At

I'm using SoapCore (SOAP protocol middleware for ASP.NET Core) to create a Soap Server using an existing soap file (.WSDL) on my local machine.

I'm using the following configuration in appsettings.json:

"FileWSDL":
  {
    "UrlOverride": "/Service.asmx",
    "WebServiceWSDLMapping":
    {
      "Service.asmx":
      {
        "WsdlFile": "SOAP.wsdl",
        "SchemaFolder": "D: /",
        "WsdlFolder": "D: /"
      }
    }
  },
    "VirtualPath": "",

The problem is that when I try to run the application I get the following error:

An unhandled exception occurred while processing the request.
XmlException: Root element is missing.
System.Xml.XmlTextReaderImpl.Throw (Exception e)

Some help?

I'm using ASP.NET Core 3.1.

My Startup.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Models;
using SoapCore;
using Microsoft.Extensions.Configuration;

namespace SoapServer
{
    public class Startup
    {
        private readonly IConfiguration Configuration;

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.TryAddSingleton<ISampleService, SampleService>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var settings = Configuration.GetSection("FileWSDL").Get<WsdlFileOptions>();
            settings.AppPath = env.ContentRootPath; // The hosting environment root path

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });

            app.UseSoapEndpoint<ISampleService>("/Service.asmx", new BasicHttpBinding(), SoapSerializer.XmlSerializer, false, null, settings);
        }
    }
}
2

There are 2 answers

0
Athanasios Kataras On

All xml files (wsdl files are xml too) must have a root element: https://www.w3schools.com/xml/xml_syntax.asp

XML documents must contain one root element that is the parent of all other elements

Valid Example:

<root>
  <child>
    <subchild>.....</subchild>
  </child>
  <child>
    <subchild>.....</subchild>
  </child>
</root>

InValid Example:

  <child>
    <subchild>.....</subchild>
  </child>
  <child>
    <subchild>.....</subchild>
  </child>

So it's either that your wsdl is invalid or that the application can't access it and you are using an empty file.

0
Kelly Kimble On

I had this same issue and my XML was fine - it just wasn't finding it.
I solved it by setting the AppPath value on the settings object:

var settings = Configuration.GetSection("FileWSDL").Get<WsdlFileOptions>();            
settings.AppPath = AppDomain.CurrentDomain.BaseDirectory; 
app.UseSoapEndpoint<Models.RateLinxWS>("/Shipping.asmx", new BasicHttpBinding() { Name = "RateLinxWSSoap", Namespace = "RateLinxWSSoap" }, SoapSerializer.XmlSerializer, false,null, settings);