我需要使用XmWriter在元素中使用多个xmlns元素

6 c# xml xsd xmlwriter xml-namespaces

我正在尝试将xml文档从一种格式转换为另一种格式,并且在执行此操作时,我发现我需要向根元素插入多个xmlns声明.

例:

<?xml version ="1.0"encoding ="utf-8"?>
<Template xmlns ="http://tempuri.org/TemplateBase.xsd"xmlns:TYPES ="http://tempuri.org/TemplateTypes.xsd ">
一些内容
<Template>

所有这一切的原因是我已经将XSD架构划分为多个XSD,以便在这种情况下重用一般类型.

好吧,我现在要做的是用XmlTextWriter编写这个xml但是我不能为TYPES编写xmlns属性.

到目前为止我尝试过的是:

XmlWriter xmlWriter = XmlWriter.Create(filename, settings);  
xmlWriter.WriteStartElement("Template", "http://tempuri.org/TemplateBase.xsd");
xmlWriter.WriteAttributeString("xmlns", "TYPES", "http://tempuri.org/TemplateTypes.xsd", XmlSchema.InstanceNamespace);
Run Code Online (Sandbox Code Playgroud)

当我执行此代码时,我得到以下异常:
System.ArgumentException:前缀"xmlns"保留供XML使用.

有没有人治愈我目前的头痛?

Doc*_*own 9

使用

xmlWriter.WriteAttributeString("xmlns", "TYPES", 
    null, "http://tempuri.org/TemplateTypes.xsd");
Run Code Online (Sandbox Code Playgroud)

代替

 xmlWriter.WriteAttributeString("xmlns", "TYPES", 
    "http://tempuri.org/TemplateTypes.xsd", XmlSchema.InstanceNamespace);
Run Code Online (Sandbox Code Playgroud)

这应该给你想要的输出.