我正在尝试使用 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">
我遇到的问题是我得到了异常:
前缀“不能在同一开始内从”重新定义
元素标签。
这是我的 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 消息的正文发送,并且我需要它采用这种格式供接收者使用。
EDIT:我在另一个问题上找到了答案。控制 XML 命名空间的顺序 https://stackoverflow.com/questions/724976/controlling-the-order-of-xml-namepaces?rq=1
您需要指出该元素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")
);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)