0

我将 xml 存储在字符串中,并使用 Xdocument 将字符串解析为 xml,我需要从中获取 xml 元素值并使用该值将其插入到数据库中。任何帮助,将不胜感激。

XML:

<ListInventorySupplyResponse xmlns="http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/">
- <ListInventorySupplyResult>
- <InventorySupplyList>
- <member>
  <SellerSKU>043859634910</SellerSKU> 
  <FNSKU>X000IA4045</FNSKU> 
  <ASIN>B005YV4DJO</ASIN> 
  <Condition>NewItem</Condition> 
  <TotalSupplyQuantity>10</TotalSupplyQuantity> 
  <InStockSupplyQuantity>10</InStockSupplyQuantity> 
- <EarliestAvailability>
  <TimepointType>Immediately</TimepointType> 
  </EarliestAvailability>
  <SupplyDetail /> 
  </member>
  </InventorySupplyList>
  </ListInventorySupplyResult>
- <ResponseMetadata>
  <RequestId>d50af29d-f203-4efc-a864-1725a59ded97</RequestId> 
  </ResponseMetadata>
  </ListInventorySupplyResponse>

代码:

XDocument xd = XDocument.Parse(a);
string Sku = xd.Element();
var ASIN = xd.Descendants("ASIN");
var Condition = xd.Descendants("Condition");
var TotalSupplyQuantity = xd.Descendants("TotalSupplyQuantity");
4

2 回答 2

2

您应该使用 xml 命名空间http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/

var xDoc = XDocument.Parse(xml);
XNamespace ns = "http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/";

var condition = (string)xDoc.Descendants(ns + "Condition").First();

或者

您可以在任何xml 命名空间中搜索Tag Condition

var condition2 = (string)xDoc.Descendants()
                             .First(d => d.Name.LocalName == "Condition");

或者

您可以使用 XPath在任何xml 命名空间中获取Condition

var condition3 = (string)xDoc.XPathSelectElement("//*[local-name()='Condition']");
于 2013-10-18T12:04:31.303 回答
1

用这个:

string value = xd.Root.Element("SellerSKU").Value;
于 2013-10-18T11:40:14.253 回答