首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >XmlException:.NET内核中缺少根元素

XmlException:.NET内核中缺少根元素
EN

Stack Overflow用户
提问于 2021-02-06 10:25:09
回答 2查看 1.3K关注 0票数 0

我正在使用SoapCore ( ASP.NET Core的Soap协议中间件)来使用本地机器上现有的soap文件(.WSDL)创建soap服务器。

我在appsettings.json中使用以下配置

代码语言:javascript
复制
"FileWSDL":
  {
    "UrlOverride": "/Service.asmx",
    "WebServiceWSDLMapping":
    {
      "Service.asmx":
      {
        "WsdlFile": "SOAP.wsdl",
        "SchemaFolder": "D: /",
        "WsdlFolder": "D: /"
      }
    }
  },
    "VirtualPath": "",

问题是,当我试图运行应用程序时,我会得到以下错误:

处理请求时发生了未处理的异常。

XmlException:根元素丢失。

System.Xml.XmlTextReaderImpl.Throw (例外e)

有人帮忙吗?

我正在使用ASP.NET Core3.1。

我的Startup.cs

代码语言:javascript
复制
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);
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2021-02-06 11:04:42

所有xml文件(wsdl文件也是xml )必须有一个根元素:https://www.w3schools.com/xml/xml_syntax.asp

XML文档必须包含一个根元素,该根元素是所有其他元素的父元素。

有效的例子:

代码语言:javascript
复制
<root>
  <child>
    <subchild>.....</subchild>
  </child>
  <child>
    <subchild>.....</subchild>
  </child>
</root>

InValid示例:

代码语言:javascript
复制
  <child>
    <subchild>.....</subchild>
  </child>
  <child>
    <subchild>.....</subchild>
  </child>

所以,要么您的wsdl无效,要么应用程序无法访问它,而且您正在使用一个空文件。

票数 0
EN

Stack Overflow用户

发布于 2021-02-12 19:56:12

我也遇到了同样的问题,我的XML很好--只是找不到它。

我通过设置设置对象的AppPath值来解决这个问题:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66075801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档