gpt4 book ai didi

c# - 前缀 "cannot be redefined from "在同一个开始元素标签中

转载 作者:IT王子 更新时间:2023-10-29 04:09:45 26 4
gpt4 key购买 nike

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

<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 prefix " cannot be redefined from " to within the same start element tag.

这是我的 C# 代码:

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 消息的主体发送,我需要它采用这种格式供接收方使用。

编辑: 我在另一个问题上找到了答案。 Controlling the order of XML namepaces

最佳答案

您需要指明元素 Foo 是命名空间 http://schemas.foo.com 的一部分。试试这个:

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

关于c# - <url> 前缀 "cannot be redefined from "在同一个开始元素标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23698767/

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