小编Ell*_*ott的帖子

在前缀元素下没有xmlns的XmlDocument CreateElement

我正在尝试通过使用以下代码中的C#XmlDocument类来编写一个SOAP请求来ebay FindingAPI Web服务:

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";
Run Code Online (Sandbox Code Playgroud)

并且,上面代码的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 xmlns="">
            <keywords>harry potter phoenix</keywords>
        </findItemsByKeywordsRequest>
    </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

但是,由于findItemsByKeywordsRequest元素中的额外xmlns =""属性,服务器无法识别此XML.所需的XML输出应如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

有谁知道我的代码有什么问题,请给我一些提示.谢谢!

c# xml xmldocument prefix xml-namespaces

1
推荐指数
1
解决办法
4551
查看次数

标签 统计

c# ×1

prefix ×1

xml ×1

xml-namespaces ×1

xmldocument ×1