请检查下面的 c# 代码。在这里,我从以下 xml 示例中获取 UPC、EAN 值的值。但当前代码的问题是有一段时间 UPC,EAN 值在 xml 中不可用,然后我得到错误 - 对象未设置为实例......所以我的问题是我如何在尝试之前检查 UPC EAN 如果为空抢夺它的价值?所以我没有得到这样的缺失数据错误?有什么解决办法吗?
C# 代码:
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
responseStream.Flush();
responseStream.Close();
XDocument doc = XDocument.Parse(responseStr);
XNamespace ns = doc.Root.GetDefaultNamespace();
string UPC = "";
string EAN = "";
if (doc.Descendants(ns + "Ack").FirstOrDefault().Value != "Failure")
{
UPC = doc.Descendants(ns + "UPC").FirstOrDefault().Value;
EAN = doc.Descendants(ns + "EAN").FirstOrDefault().Value;
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<GetItemResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2018-08-04T09:36:11.502Z</Timestamp>
<Ack>Success</Ack>
<Version>1069</Version>
<Build>E1069_CORE_API_18748854_R1</Build>
<Item>
<ProductListingDetails>
<ISBN>Does not apply</ISBN>
<UPC>732030007789</UPC>
<BrandMPN>
<Brand>Ommani</Brand>
<MPN>H0149-OM</MPN>
</BrandMPN>
<IncludeeBayProductDetails>true</IncludeeBayProductDetails>
</ProductListingDetails>
</Item>
</GetItemResponse>
错误图片:
波斯汪
相关分类