错误:无法重新定义名称空间的前缀''用于当前元素

时间:2014-12-09 02:58:44

标签: c# .net xml linq

我正在使用Linq向Xamarin发送肥皂请求。我一直在收到错误。我知道它来自 这一行:

    (new XElement ("Subscribe",
        new XAttribute ("xmlns", "eAuthService"),

非常感谢任何帮助。我尝试了一切。

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
XNamespace soap12 = "http://www.w3.org/2003/05/soap-envelope";
XNamespace eauth = "eAuthService"; 
XName xmlns = "xmlns"; 
XDocument xDoc = new XDocument (
    new XElement (soap12 + "Envelope",
        new XAttribute (XNamespace.Xmlns + "xsi", xsi),
        new XAttribute (XNamespace.Xmlns + "xsd", xsd),
        new XAttribute (XNamespace.Xmlns + "soap12", soap12), 
        new XElement (soap12 + "Body",
            (new XElement ("Subscribe",
                new XAttribute ("xmlns", "eAuthService"),
                    (new XElement ("RequestSubscription",
                        (new XElement ("Token", "Subscription")),
                        (new XElement ("ID_Subscription", myResources.registerationId)), 
                        (new XElement ("DateOfBirth", main.pickDate.Text)),
                        (new XElement ("CountryCode3Letter", "IND")))))))));

1 个答案:

答案 0 :(得分:1)

它失败了,因为在已经在不同的名称空间(空名称)下编写了没有前缀(<Subscribe>)的元素之后,您重新定义了默认名称空间(无前缀)。您可以通过执行new XElement(eAuth + "Subscribe")来解决此问题,但其下的其他元素不会自动采用eAuth命名空间,这不是我认为您想要的。

我猜你想要为<Subscribe>及其所有孩子使用eAuth名称空间 - 几乎所有不属于肥皂信封的部分。为此,您需要使用eAuth为您自己的所有节点做好准备。

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
XNamespace soap12 = "http://www.w3.org/2003/05/soap-envelope";
XNamespace eauth = "eAuthService";
XDocument xDoc = new XDocument (
    new XElement (soap12 + "Envelope",
        new XAttribute (XNamespace.Xmlns + "xsi", xsi),
        new XAttribute (XNamespace.Xmlns + "xsd", xsd),
        new XAttribute (XNamespace.Xmlns + "soap12", soap12), 
        new XElement (soap12 + "Body",
            (new XElement (eauth + "Subscribe",
                new XAttribute ("xmlns", "eAuthService"),
                (new XElement (eauth + "RequestSubscription",
                    (new XElement (eauth + "Token", "Subscription")),
                    (new XElement (eauth + "ID_Subscription", myResources.registrationId)), 
                    (new XElement (eauth + "DateOfBirth", main.pickDate.Text)),
                    (new XElement (eauth + "CountryCode3Letter", "IND")))))))));