如何获得XML元素值使用的XDocument字符串字符串、如何获得、元素、XML

2023-09-03 07:47:44 作者:醉话见心

我存储在字符串中XML和使用的XDocument我解析从我需要得到XML元素值到XML字符串,并使用该值我需要将其插入数据库。任何帮助将是AP preciated。

XML:

 < ListInventorySupplyResponse的xmlns =htt​​p://mws.amazonaws.com/FulfillmentInventory/2010-10-01/>
 - < ListInventorySupplyResult>
 - < InventorySupplyList>
 - <成员>
  < SellerSKU> 043859634910< / SellerSKU>
  < FNSKU> X000IA4045< / FNSKU>
  < ASIN> B005YV4DJO< / ASIN>
  <条件>的newitem&所述; /条件>
  &其中; TotalSupplyQuantity→10&所述; / TotalSupplyQuantity>
  &其中; InStockSupplyQuantity→10&所述; / InStockSupplyQuantity>
 - < EarliestAvailability>
  < TimepointType>立即< / TimepointType>
  < / EarliestAvailability>
  < SupplyDetail />
  < /件>
  < / InventorySupplyList>
  < / ListInventorySupplyResult>
 - < ResponseMetadata>
  <请求ID> d50af29d-f203-4efc-a864-1725a59ded97< /请求ID>
  < / ResponseMetadata>
  < / ListInventorySupplyResponse>
 

code:

 的XDocument XD = XDocument.Parse(一);
字符串SKU = xd.Element();
变种ASIN = xd.Descendants(ASIN);
VAR条件= xd.Descendants(条件);
变种TotalSupplyQuantity = xd.Descendants(TotalSupplyQuantity);
 

解决方案 Android移动开发技术文章 手机开发

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

  XDOC VAR = XDocument.Parse(XML);
的XNamespace NS =htt​​p://mws.amazonaws.com/FulfillmentInventory/2010-10-01/;

。VAR条件=(字符串)xDoc.Descendants(NS +条件)第一();
 

您可以搜索标签的条件的中的任意的XML命名空间

  VAR条件2 =(字符串)xDoc.Descendants()
                             。首先(D => d.Name.LocalName ==条件);
 

您可以使用XPath获得的条件的中的任意的XML命名空间

  VAR condition3 =(字符串)xDoc.XPathSelectElement(// * [本地名称()='条件']);
 

I am storing a xml in a string and using Xdocument i am parsing the string to xml from that i need to get xml element values and using that values i need to insert it in db. Any help would be appreciated.

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>

Code:

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

解决方案

You should use the xml namespace 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();

OR

you can search for Tag Condition in any xml namespace

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

OR

you can use XPath to get Condition in any xml namespace

var condition3 = (string)xDoc.XPathSelectElement("//*[local-name()='Condition']");