前缀“不能从“重新定义"到 <url>在同一个开始元素标签内 [英] The prefix &quot; cannot be redefined from &quot; to &lt;url&gt; within the same start element tag

查看:30
本文介绍了前缀“不能从“重新定义"到 <url>在同一个开始元素标签内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C# 生成以下 xml 元素.

I'm trying to generate the following xml element using C#.

<Foo xmlns="http://schemas.foo.com" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://schemas.foo.com
 http://schemas.foo.com/Current/xsd/Foo.xsd">

我遇到的问题是出现异常:

The problem that I'm having is that I get the exception:

前缀不能从"重新定义到同一个开头元素标签.

The prefix " cannot be redefined from " to within the same start element tag.

这是我的 C# 代码:

This is my c# code:

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XElement foo = new XElement("Foo", new XAttribute("xmlns", "http://schemas.foo.com"),
                                   new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
                                   new XAttribute(xsi + "schemaLocation", "http://schemas.foo.com http://schemas.foo.com/Current/xsd/Foo.xsd"));

我该如何解决这个问题?我正在尝试将生成的 xml 作为 SOAP 消息的正文发送,我需要它采用这种格式以供接收者使用.

How can I fix this? I'm trying to send the generated xml as the body of a SOAP message and I need it to be in this format for the receiver.

我在另一个问题上找到了答案.控制 XML 命名空间的顺序

I found my answer on another question. Controlling the order of XML namepaces

推荐答案

您需要指出元素 Foo 是命名空间 http://schemas.foo.com<的一部分/代码>.试试这个:

You need to indicate that the element Foo is part of the namespace http://schemas.foo.com. Try this:

XNamespace xNamespace = "http://schemas.foo.com";    
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XElement foo = new XElement(
    xNamespace + "Foo", 
    new XAttribute("xmlns", "http://schemas.foo.com"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    new XAttribute(xsi + "schemaLocation", "http://schemas.foo.com http://schemas.foo.com/Current/xsd/Foo.xsd")
    );

这篇关于前缀“不能从“重新定义"到 <url>在同一个开始元素标签内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆