【问题标题】:XElement Automatically Having xmlns attributeXElement 自动具有 xmlns 属性
【发布时间】:2014-09-30 07:41:23
【问题描述】:
Dim soapEnvelope As XElement = New XElement(soap + "Envelope",
                                        New XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
                                        New XAttribute(soap + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding"),
                                        New XElement(soap + "Body",
                                        New XAttribute("xmlns", "http://www.test.com"),
                                        New XElement("Open",
                                        New XElement("Data",
                                        New XElement("Desc", _dData.Desc),
                                        New XElement("Num", _dData.Num),
                                        New XElement("Ref", _dData.Ref),
                                        New XElement("Mnn", _dData.Mnn),
                                        New XElement("Ftp", _dData.Ftp))
                                  )))

下面给出这个输出:

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
  <soap:Body xmlns="http://www.test.com">
    <Open xmlns="">
      <Data>
        <Desc>testApp</Desc>
        <Num>1</Num>
        <Ref></Ref>
        <Mnn>116</Mnn>
        <Ftp></Ftp>
      </Data>
    </Open>
  </soap:Body>
</soap:Envelope>

问题是为什么&lt;Open&gt; XElement 会自动获得xmlns="" 属性?

我想要相同的输出,但没有 XElement &lt;Open&gt; 的任何属性

任何帮助将不胜感激。

【问题讨论】:

    标签: vb.net linq-to-xml xelement soapheader xattribute


    【解决方案1】:

    这是因为 XML 在 &lt;Open&gt; 元素中声明了默认命名空间 (xmlns="...")。在 XML 中,所有后代元素都自动从祖先继承默认命名空间,除非另有明确设置(例如,通过使用指向不同命名空间的前缀,或通过在后代级别声明不同的默认命名空间)。

    使用您尝试过的代码,&lt;Body&gt; 的后代未设置在命名空间中。您需要使用XNamespace&lt;Open&gt; 元素的后代设置在相同的默认命名空间中,例如:

    XNamespace ns = "http://www.test.com"
    Dim soapEnvelope As XElement = New XElement(soap + "Envelope",
                                            New XAttribute(XNamespace.Xmlns + "soap", soap.NamespaceName),
                                            New XAttribute(soap + "encodingStyle", "http://www.w3.org/2001/12/soap-encoding"),
                                            New XElement(soap + "Body",
                                            New XAttribute("xmlns", "http://www.test.com"),
                                            New XElement(ns+"Open",
                                            New XElement(ns+"Data",
                                            New XElement(ns+"Desc", _dData.Desc),
                                            New XElement(ns+"Num", _dData.Num),
                                            New XElement(ns+"Ref", _dData.Ref),
                                            New XElement(ns+"Mnn", _dData.Mnn),
                                            New XElement(ns+"Ftp", _dData.Ftp))
                                      )))
    

    【讨论】:

      【解决方案2】:

      您需要在其命名空间中创建每个元素:

      XNamespace t = "http://www.test.com";
                                            New XElement(t + "Open",
                                              New XElement(t + "Data",
                                              New XElement(t + "Desc", _dData.Desc),
                                              New XElement(t + "Num", _dData.Num),
                                              New XElement(t + "Ref", _dData.Ref),
                                              New XElement(t + "Mnn", _dData.Mnn),
                                              New XElement(t + "Ftp", _dData.Ftp))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 1970-01-01
        • 2012-08-15
        • 1970-01-01
        相关资源
        最近更新 更多