我尝试使用传统的 XElement 和 XAttribute 类从 C# 动态创建 XML 架构 (XSD),但使用冒号指定任何名称都是无效的。也就是说,我无法使用代码创建元素
... = new XElement("xs:element");
因为“:”是不允许的。
那么在 C# 中动态构建架构的正确方法是什么?
最佳答案
要创建架构,您应该使用 XmlSchema 类。下面的链接提供了以编程方式创建的综合示例:
http://msdn.microsoft.com/en-us/library/9ta3w88s.aspx
示例:
static void Main(string[] args)
{
var schema = new XmlSchema();
// <xs:element name="myElement" type="xs:string"/>
var myElement = new XmlSchemaElement();
schema.Items.Add(myElement);
elementCat.Name = "myElement";
elementCat.SchemaTypeName =
new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// writing it out to any stream
var nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
schema.Write(Console.Out, nsmgr);
Console.ReadLine();
}
关于c# - 如何在 C# 中动态创建 XML 架构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5473975/