XmlDocument CreateElement without xmlns under a prefixed element

后端 未结 1 1543
广开言路
广开言路 2020-12-21 18:03

I\'m trying to compose a SOAP request to ebay FindingAPI web service by using C# XmlDocument class in the following code:

XmlDocument doc = new XmlDocument()         


        
相关标签:
1条回答
  • 2020-12-21 18:43

    Because your document has default namespace declared in the most outer element you have to repeat that namespace on every child element to avoid adding additional empty one.

    Change request and param elements declaration to contain "http://www.ebay.com/marketplace/search/v1/services" namespace

    XmlElement request = (XmlElement)body.AppendChild(doc.CreateElement("findItemsByKeywordsRequest", "http://www.ebay.com/marketplace/search/v1/services"));
    XmlElement param = (XmlElement)request.AppendChild(doc.CreateElement("keywords", "http://www.ebay.com/marketplace/search/v1/services"));
    

    With these changes your code produces following XML:

    <soap:Envelope xmlns="http://www.ebay.com/marketplace/search/v1/services" xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
        <soap:Header />
        <soap:Body>
            <findItemsByKeywordsRequest>
                <keywords>harry potter phoenix</keywords>
            </findItemsByKeywordsRequest>
        </soap:Body>
    </soap:Envelope>
    
    0 讨论(0)
提交回复
热议问题