Question

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();
XmlElement root = (XmlElement)doc.AppendChild(doc.CreateElement("soap", "Envelope", "http://www.w3.org/2003/05/soap-envelope"));
root.SetAttribute("xmlns", "http://www.ebay.com/marketplace/search/v1/services");
XmlElement header = (XmlElement)root.AppendChild(doc.CreateElement("soap", "Header", "http://www.w3.org/2003/05/soap-envelope"));
XmlElement body = (XmlElement)root.AppendChild(doc.CreateElement("soap", "Body", "http://www.w3.org/2003/05/soap-envelope"));
XmlElement request = (XmlElement)body.AppendChild(doc.CreateElement("findItemsByKeywordsRequest"));
XmlElement param = (XmlElement)request.AppendChild(doc.CreateElement("keywords"));
param.InnerText = "harry potter phoenix";

And, the XML output of above code is:

<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 xmlns="">
            <keywords>harry potter phoenix</keywords>
        </findItemsByKeywordsRequest>
    </soap:Body>
</soap:Envelope>

However, this XML can't be recognized by the server because of the extra xmlns="" attribute in the findItemsByKeywordsRequest element. The desired XML output should be as below:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns="http://www.ebay.com/marketplace/search/v1/services">
    <soap:Header/>
    <soap:Body>
        <findItemsByKeywordsRequest>
            <keywords>harry potter phoenix</keywords>
        </findItemsByKeywordsRequest>
    </soap:Body>
</soap:Envelope>

Does anyone know what is the problem of my code and please give me some hints. Thanks!

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top