如何在MVC Core(1.0.0)中读取xml文档? [英] How to read xml document in MVC core (1.0.0)?

查看:87
本文介绍了如何在MVC Core(1.0.0)中读取xml文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web编程的新手,因此决定从.net 4.5切换到.net core.

I'm new to web programming and decided to switch from .net 4.5 to .net core.

我的项目在以下位置有一个静态xml文档:

My project has a static xml document in the following location:

wwwroot/Countries/en-GB.xml

wwwroot/Countries/en-GB.xml

如何读取指定路径下的xml文件?最终,我将数据转换为一个SelectList.

How would one go about reading the xml file at the specified path? Eventually I will convert the data to a SelectList.

在.net 4.5中,我使用了DataSet和HttpConext ... MapPath来读取xml文档,该文档不再在核心mvc中工作.

In .net 4.5 I used DataSet's and HttpConext...MapPath to read the xml document which no longer works in core mvc.

任何建议都将受到欢迎.

Any advice is greatly welcome.

推荐答案

首先不要将数据源放入wwwroot文件夹,因为它是公开提供的.看看官方文档:

First of all don't put your data source into wwwroot folder, because it is served publicly. Take a look at official docs:

您应用的网络根目录是项目中公共目录, 静态资源,例如CSS,JS和图片文件.静态文件 中间件将仅提供Web根目录中的文件(和 子目录).

The web root of your app is the directory in your project for public, static resources like css, js, and image files. The static files middleware will only serve files from the web root directory (and sub-directories) by default.

因此将Countries文件夹移到项目的根文件夹中.

So move Countries folder in your project's root folder.

要读取xml数据,可以使用XmlSerializer.我将尝试展示如何读取xml文件:

To read xml data, you can use XmlSerializer. I will try to show how to read xml file:

首先,我假设您具有如下所示的xml内容:

First i assume you have xml content like below:

<?xml version="1.0" encoding="UTF-8" ?>
<Container>
  <Countries>
    <Country>
      <Code>Code1</Code>
      <Title>Title1</Title>
    </Country>

    <Country>
      <Code>Code2</Code>
      <Title>Title2</Title>
    </Country>
  </Countries>
</Container>

首先描述类型

public class Country
{
    public string Code { get; set; }
    public string Title { get; set; }
}
public class Container
{
    public Country[] Countries { get; set; }
}

之后,创建一个用于XML反序列化的服务:

After that create a service for xml deserialization:

public interface ICountryService
{
    Country[] GetCountries();
}
public class CountryService : ICountryService
{
    private readonly IHostingEnvironment _env;
    public CountryService(IHostingEnvironment env)
    {
        _env = env;
    }
    public Country[] GetCountries()
    {
        XmlSerializer ser = new XmlSerializer(typeof(Container));
        FileStream myFileStream = new FileStream(_env.ContentRootPath + "\\Countries\\en-GB.xml", FileMode.Open);
        return ((Container)ser.Deserialize(myFileStream)).Countries;
    }
}

然后使用ConfigureServices方法注册服务:

Then register service in ConfigureServices method:

    public void ConfigureServices(IServiceCollection services)
    {
        // ...
        services.AddSingleton<ICountryService, CountryService>();
    }

最后在任何地方(例如在控制器中)注入并使用它

Finally inject and use it anywhere(such as in a controller)

public class SomeController : Controller
{
    public SomeController(ICountryService countryService)
    {
         // use it
    }
}

这篇关于如何在MVC Core(1.0.0)中读取xml文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆