我将xml存储在字符串中并使用Xdocument我正在将字符串解析为xml,我需要获取xml元素值并使用该值我需要将其插入db中。任何帮助将不胜感激。
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");
答案 0 :(得分: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
条件
var condition2 = (string)xDoc.Descendants()
.First(d => d.Name.LocalName == "Condition");
或
您可以使用XPath在任何 xml命名空间中获取条件
var condition3 = (string)xDoc.XPathSelectElement("//*[local-name()='Condition']");
答案 1 :(得分:1)
使用此:
string value = xd.Root.Element("SellerSKU").Value;