在同一个start元素标记中,前缀“无法从”重新定义为

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

 

我遇到的问题是我得到了exception:前缀“无法从”重新定义到同一个start元素标记内。 这是我的c#代码:

 XNamespace xsi = "http://www.w3.org/2001/XMLScema-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消息的主体发送,我需要它以接收器的这种格式。

编辑:我在另一个问题上找到了答案。 控制XML名称空间的顺序

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

 XNamespace xNamespace = "http://schemas.foo.com"; XNamespace xsi = "http://www.w3.org/2001/XMLScema-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") );