gpt4 book ai didi

c# - 我需要在一个元素中使用 XmWriter 的多个 xmlns 元素

转载 作者:数据小太阳 更新时间:2023-10-29 01:50:06 24 4
gpt4 key购买 nike

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

例子:


所有这一切的原因是我将 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);

当我执行此代码时,出现以下异常:
System.ArgumentException:前缀“xmlns”保留供 XML 使用。

有没有人能治好我现在的头痛?

最佳答案

使用

xmlWriter.WriteAttributeString("xmlns", "TYPES", 
null, "http://tempuri.org/TemplateTypes.xsd");

代替

 xmlWriter.WriteAttributeString("xmlns", "TYPES", 
"http://tempuri.org/TemplateTypes.xsd", XmlSchema.InstanceNamespace);

这应该会为您提供所需的输出。

关于c# - 我需要在一个元素中使用 XmWriter 的多个 xmlns 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2501755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com