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

发布于 2024-08-26 06:03:27 字数 792 浏览 4 评论 0原文

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

示例:


<模板 xmlns="http://tempuri.org/TemplateBase.xsd" xmlns:TYPES="http://tempuri.org/TemplateTypes.xsd">
部分内容
<模板>

所有这一切的原因是我已将 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 使用。

有人能治愈我当前的头痛吗?

I'm trying to convert a xml document from one format to another and while doing this I've found that I need to insert multiple xmlns declarations to the root element.

Example:

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

The reason of all this is that I've divided the XSD schema into multiple XSD in order to reuse the general types in this case.

Well, what I want to do now is to write this xml with a XmlTextWriter but I can't write the xmlns attribute for the TYPES.

What I've tried so far is:

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);

When I execute this code I get the following exception:
System.ArgumentException: Prefix "xmlns" is reserved for use by XML..

Does anyone have any cure to my current headache?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

话少心凉 2024-09-02 06:03:27

使用

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

而不是

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

这应该会给你想要的输出。

Use

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

instead of

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

This should give you the desired output.

秋千易 2024-09-02 06:03:27

这很简单。不要编写 xmlns 属性。

相反,您应该在属性和元素所属的命名空间中编写属性和元素。XmlWriter 将自行处理命名空间声明(xmlns 属性)。

It's very simple. Don't write the xmlns attributes.

Instead, you should be writing your attributes and elements in the namespace they belong in. XmlWriter will take care of the namespace declarations (xmlns attributes) on its own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文