2

我需要存储节点 "member" 内的元素值。我已经尝试了以下代码,但我无法实现。如何获取值。任何帮助,将不胜感激

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>7</TotalSupplyQuantity>
        <InStockSupplyQuantity>7</InStockSupplyQuantity>
        <EarliestAvailability>
          <TimepointType>Immediately</TimepointType>
        </EarliestAvailability>
        <SupplyDetail>
        </SupplyDetail>
      </member>
    </InventorySupplyList>
  </ListInventorySupplyResult>
  <ResponseMetadata>
    <RequestId>58c9f4f4-6f60-496a-8d71-8fe99ce301c9</RequestId>
  </ResponseMetadata>
</ListInventorySupplyResponse>

C#代码:

string a = Convert.ToString(oInventorySupplyRes.ToXML());
 XmlDocument oXdoc = new XmlDocument();
 oXdoc.LoadXml(a);
 XmlNodeList oInventorySupplyListxml = oXdoc.SelectNodes("//member");
 foreach (XmlNode itmXml in oInventorySupplyListxml)
 {
    // var cond = itmXml.InnerXml.ToString();
     var asinVal = itmXml.SelectSingleNode("ASIN").Value;
     var TotalSupplyQuantityVal = itmXml.SelectSingleNode("TotalSupplyQuantity").Value;
 }

结果视图 : "Enumeration yielded no results" and count = 0;

编辑1:

 string a = Convert.ToString(oInventorySupplyRes.ToXML());
                var status = oInventorySupplyResult.InventorySupplyList;
                XmlDocument oXdoc = new XmlDocument();
                var doc = XDocument.Parse(a);
                var r = doc.Descendants("member")
                           .Select(member => new
                           {
                               ASIN = member.Element("ASIN").Value,
                               TotalSupplyQuantity = member.Element("TotalSupplyQuantity").Value
                           });
4

3 回答 3

1
private string mStrXMLStk = Application.StartupPath + "\\Path.xml";
private System.Xml.XmlDocument mXDoc = new XmlDocument();
mXDoc.Load(mStrXMLStk);
XmlNode XNode = mXDoc.SelectSingleNode("/ListInventorySupplyResult/InventorySupplyList/member");
if (XNode != null)
{
   int IntChildCount = XNode.ChildNodes.Count;
   for (int IntI = 1; IntI <= IntChildCount ; IntI++)
   {
        string LocalName = XNode.ChildNodes[IntI].LocalName;
        XmlNode Node = mXDoc.SelectSingleNode("/Response/" + LocalName);
        // Store Value in Array assign value by "Node.InnerText"
   }
}

试试这个Code。它的工作

于 2016-01-02T17:25:15.267 回答
0

我想这会对你有所帮助..

 string a = Convert.ToString(oInventorySupplyRes.ToXML());
 XmlDocument oXdoc = new XmlDocument();
 oXdoc.LoadXml(a);
 XmlNodeList fromselectors;
 XmlNodeList toselectors;
 XmlElement root = oXdoc.DocumentElement;
 fromselectors = root.SelectNodes("ListInventorySupplyResult/InventorySupplyList/member/ASIN");
 toselectors = root.SelectNodes("ListInventorySupplyResult/InventorySupplyList/member/TotalSupplyQuantity");

foreach (XmlNode m in fromselectors)
   {
      you will have value in `m.InnerXml` use it whereever you want..
   }
foreach (XmlNode n in toselectors)
   {
      you will have value in `n.InnerXml` use it whereever you want..
   }
于 2013-10-22T09:30:40.247 回答
0

尝试使用这个 xpath

string xPath ="ListInventorySupplyResponse/ListInventorySupplyResult
           /InventorySupplyList/member"
XmlNodeList oInventorySupplyListxml = oXdoc.SelectNodes(xpath);

当您这样做时"//member",代码会尝试查找member从根级别命名的元素,该元素不存在于根级别,而是嵌套在几层之下。

于 2013-10-22T07:27:24.173 回答