0
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>

問題は、なぜ<Open>XElement が自動的にxmlns="" 属性を取得したのかということです。

同じ出力が必要ですが、XElement への属性はありません<Open>

どんな助けでも大歓迎です。

4

2 に答える 2

1

これは、XML の要素でデフォルトの名前空間 ( xmlns="...") が宣言されているためです<Open>。XML では、すべての子孫要素は、特に明示的に設定されていない限り (別の名前空間を指すプレフィックスを使用するか、子孫レベルで別の既定の名前空間を宣言することによって)、祖先から自動的に既定の名前空間を継承します。

試したコードを使用すると、 の子孫は<Body>名前空間に設定されません。<Open>を使用して、要素の子孫を同じデフォルト名前空間に設定する必要がありますXNamespace。次に例を示します。

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))
                                  )))
于 2014-09-30T08:10:39.107 に答える