【问题标题】:How to declare a xsd with XML.Linq in C#?如何在 C# 中使用 XML.Linq 声明 xsd?
【发布时间】:2018-07-18 12:55:40
【问题描述】:

我正在创建一个应用程序,我需要使用 c# 创建一个 XML 文件。 我目前正在使用 xml.linq 包。

XML 需要一些 xsd 声明,如图片第二行所示。我不知道怎么去那里。已经尝试了一些 Xname oder XAttribute 但它并没有完全按照我想要的方式工作。

XML how it needs to look like

【问题讨论】:

    标签: c# xml xsd


    【解决方案1】:

    命名空间可能很难添加。我发现只解析一个字符串来获得结果更容易。见下方代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication53
    {
        class Program
        {
    
            static void Main(string[] args)
            {
    
                string ident = "<?xml version=\"1.0\"?><c_TemplaceSaver xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"></c_TemplaceSaver>";
    
                XDocument doc = XDocument.Parse(ident);
                XElement c_TemplateSaver = doc.Root;
                c_TemplateSaver.Add(new XElement("ABC", 123));
    
            }
    
        }
    }
    

    【讨论】:

    • 非常感谢,这就是它的工作方式。不知道可以解析这样的字符串。总是试图用命名空间来解决这个问题,但效果并不好。谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多