【问题标题】:generate xml file pre defined schema in mvc C# from list [closed]从列表中生成 xml 文件在 mvc C# 中预定义的模式 [关闭]
【发布时间】:2020-09-08 00:49:10
【问题描述】:

在 mvc c# 过程中使用模式创建 xml 文件

【问题讨论】:

  • 架构是 XML 格式,因此您可以使用任何 xml 库类来生成架构。
  • 如果您提供更多详细信息、代码 - 您遇到什么错误以及预期输出是什么,那就太好了。这没什么好说的。只需通过最小的上下文,您就可以使用 XmlSerialization 生成您的 XML,然后将其保存为文件。

标签: c# xml model-view-controller schema


【解决方案1】:

您可以使用 System.Xml.Linq.XNamespace 来使用 Xml Schema。 示例方法如下:

    [HttpGet]
    public void CreateXmlFile(string name)
    {
        XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
        XNamespace xsd = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
        XNamespace ns  = XNamespace.Get("http://schema.test.com/test");

        new XDocument(
                          new XDeclaration("1.0", "utf-8", "yes"),
                          new XElement(

                              ns + "root",
                              new XAttribute("xmlns", ns.NamespaceName),
                              new XAttribute(XNamespace.Xmlns + "xsd", xsd.NamespaceName),
                              new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                              new XElement(ns + "Person",
                                 new XElement(ns + "Name", name)
                              )
                          )
                      )
                      .Save(@"Your XML Path");
    }

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 2011-04-24
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多