没有的xmlns XmlDocument的一个的createElement前缀元素 [英] XmlDocument CreateElement without xmlns under a prefixed element

查看:585
本文介绍了没有的xmlns XmlDocument的一个的createElement前缀元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下面的代码使用C#XmlDocument类组成一个SOAP请求给eBay FindingAPI Web服务:

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";

和,上面代码的XML输出​​是:

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>



然而,这个XML不能由服务器因为额外的xmlns公认=属性在findItemsByKeywordsRequest元素。期望的XML输出​​应该如下:

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!

推荐答案

由于您的文档在最外层元素声明默认命名空间,你必须重复命名空间的每个子元素上避免增加额外的空单。

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.

修改要求参数元素声明包含http://www.ebay.com/marketplace/search/v1/services命名

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"));

通过这些改变你的代码产生下面的XML:

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>

这篇关于没有的xmlns XmlDocument的一个的createElement前缀元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆