解析 eBay 的 api 结果 xml 时未找到任何元素
我正在尝试通过 C# 和 XML 搜索 eBay。我可以看到,通过将 XML 写入字符串,我获得了有效的 XML 响应,但无法使用 C# 解析它 - 我只是不断被告知没有元素。
这是我的代码,删除了我的 appname 键:
string xmldata = "<?xml version='1.0' encoding='utf-8'?>";
xmldata += "<findItemsAdvancedRequest xmlns='http://www.ebay.com/marketplace/search/v1/services'>";
xmldata += "<keywords>sneakers</keywords>";
xmldata += "<categoryId>1</categoryId>";
xmldata += "<descriptionSearch>false</descriptionSearch>";
xmldata += "<paginationInput>";
xmldata += "<entriesPerPage>5</entriesPerPage>";
xmldata += "</paginationInput>";
xmldata += "</findItemsAdvancedRequest>";
string url = "http://svcs.ebay.com/services/search/FindingService/v1";
//Create a HttpWebRequest object
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//Convert xml string to a byte array
byte[] postDataBytes = Encoding.ASCII.GetBytes(xmldata);
//Set the Method property
req.Method = "POST";
//Set the ContentType property of the "HttpWebRequest"
req.ContentType = "text/xml;charset=UTF-8";
req.Headers.Add("X-EBAY-SOA-SERVICE-NAME", "FindingService");
req.Headers.Add("X-EBAY-SOA-OPERATION-NAME", "findItemsAdvanced");
req.Headers.Add("X-EBAY-SOA-SERVICE-VERSION", "1.4.0");
req.Headers.Add("X-EBAY-SOA-GLOBAL-ID", "EBAY-GB");
req.Headers.Add("X-EBAY-SOA-REQUEST-DATA-FORMAT", "XML");
req.Headers.Add("X-EBAY-SOA-SECURITY-APPNAME: **********************");
//Set the ContentLength property of the "HttpWebRequest"
req.ContentLength = postDataBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postDataBytes, 0, postDataBytes.Length);
requestStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
XDocument myXML = XDocument.Load(resp.GetResponseStream());
IEnumerable<XElement> elements = myXML.Root.Element("searchResult").Elements("item");
我尝试了最后一行的各种组合 - 例如获取后代的计数,但总是收到“无匹配元素”消息的变体。我知道结果就在那里:当我设置断点并在 Visual Studio 中查看 myXML 变量时,XML 似乎都在根目录中。
I'm trying to search eBay via C# and XML. I can see that I'm getting a valid XML response, by writing out the XML to a string, but cannot parse it using C# - I just keep getting told that there are no elements.
Here's my code, with my appname key removed:
string xmldata = "<?xml version='1.0' encoding='utf-8'?>";
xmldata += "<findItemsAdvancedRequest xmlns='http://www.ebay.com/marketplace/search/v1/services'>";
xmldata += "<keywords>sneakers</keywords>";
xmldata += "<categoryId>1</categoryId>";
xmldata += "<descriptionSearch>false</descriptionSearch>";
xmldata += "<paginationInput>";
xmldata += "<entriesPerPage>5</entriesPerPage>";
xmldata += "</paginationInput>";
xmldata += "</findItemsAdvancedRequest>";
string url = "http://svcs.ebay.com/services/search/FindingService/v1";
//Create a HttpWebRequest object
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
//Convert xml string to a byte array
byte[] postDataBytes = Encoding.ASCII.GetBytes(xmldata);
//Set the Method property
req.Method = "POST";
//Set the ContentType property of the "HttpWebRequest"
req.ContentType = "text/xml;charset=UTF-8";
req.Headers.Add("X-EBAY-SOA-SERVICE-NAME", "FindingService");
req.Headers.Add("X-EBAY-SOA-OPERATION-NAME", "findItemsAdvanced");
req.Headers.Add("X-EBAY-SOA-SERVICE-VERSION", "1.4.0");
req.Headers.Add("X-EBAY-SOA-GLOBAL-ID", "EBAY-GB");
req.Headers.Add("X-EBAY-SOA-REQUEST-DATA-FORMAT", "XML");
req.Headers.Add("X-EBAY-SOA-SECURITY-APPNAME: **********************");
//Set the ContentLength property of the "HttpWebRequest"
req.ContentLength = postDataBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postDataBytes, 0, postDataBytes.Length);
requestStream.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
XDocument myXML = XDocument.Load(resp.GetResponseStream());
IEnumerable<XElement> elements = myXML.Root.Element("searchResult").Elements("item");
I've tried various combinations of the last line - getting the Count of the Descendants for example, but always get variations of 'no matching elements' messages. I know that the results are there: when I set a breakpoint and look at the myXML variable in Visual Studio, the XML all seems to be in the Root.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于返回的 XML 使用 XML 命名空间,而您的查询则不使用。假设命名空间与您发送到服务器的 XML 中的命名空间相同,则以下方法可行:
The problem is that the returned XML uses XML namespaces, while your query doesn't. Assuming the namespace is the same as the one in the XML you are sending to the server, this would work: