【发布时间】:2018-07-18 12:55:40
【问题描述】:
我正在创建一个应用程序,我需要使用 c# 创建一个 XML 文件。 我目前正在使用 xml.linq 包。
XML 需要一些 xsd 声明,如图片第二行所示。我不知道怎么去那里。已经尝试了一些 Xname oder XAttribute 但它并没有完全按照我想要的方式工作。
【问题讨论】:
我正在创建一个应用程序,我需要使用 c# 创建一个 XML 文件。 我目前正在使用 xml.linq 包。
XML 需要一些 xsd 声明,如图片第二行所示。我不知道怎么去那里。已经尝试了一些 Xname oder XAttribute 但它并没有完全按照我想要的方式工作。
【问题讨论】:
命名空间可能很难添加。我发现只解析一个字符串来获得结果更容易。见下方代码
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));
}
}
}
【讨论】: