在C#中从XML中检索多个值

时间:2023-02-07 23:48:10

I am trying to retrieve multiple values of and store in a list. In my Code string value is equal to this data.

我正在尝试检索多个值并存储在列表中。在我的代码中,字符串值等于此数据。

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns:x="http://schemas.microsoft.com/office/2008/07/excelservices/rest" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservice" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Part_Number</title>
  <id>http://test.net/excel/_vti_bin/ExcelRest.aspx/docs/abc.xlsm/Model/Ranges('Part_Number')</id>
  <updated>2015-04-07T16:23:43Z</updated>
  <author>
    <name />
  </author>
  <link rel="self" href="http://test.net/excel/_vti_bin/ExcelRest.aspx/docs/abc.xlsm/Model/Ranges('Part_Number')?$format=atom" title="Part_Number" />
  <category term="ExcelServices.Range" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <x:range name="Part_Number">
      <x:row>
        <x:c>
          <x:fv>Part Number</x:fv>
        </x:c>
      </x:row>
      <x:row>
        <x:c>
          <x:fv>LBL-61641-000</x:fv>
        </x:c>
      </x:row>
      <x:row>
        <x:c>
          <x:fv>HDW-61277-001</x:fv>
        </x:c>
      </x:row>
    </x:range>
  </content>
</entry>

C# Code

List<string> lirangeName = new List<string>();
string value = GetRangeValue1("abc.xlsm", lirangeName[0]);  //Value contains the above xml value
XmlDocument doc = new XmlDocument();
doc.LoadXml(value);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("x", "http://schemas.microsoft.com/office/2008/07/excelservices/rest");
string answer = doc.SelectSingleNode("//x:fv", nsmgr).InnerXml;

Here, string answer = Part Number. So, I get 1 value but I want a list of values.

这里,字符串答案=部件号。所以,我得到1个值,但我想要一个值列表。

Answer I want :
Part Number
LBL-61641-000
HDW-61277-001

Answer I get:.
Part Number

1 个解决方案

#1


0  

To get the list of nodes in the collection, change your last line to the following:

要获取集合中的节点列表,请将最后一行更改为以下内容:

var answers = doc.SelectNodes("//x:fv", nsmgr);

if (answers == null)
    return;

foreach (var item in answers)
{
     var result = ((XmlNode)item).InnerXml;
}

#1


0  

To get the list of nodes in the collection, change your last line to the following:

要获取集合中的节点列表,请将最后一行更改为以下内容:

var answers = doc.SelectNodes("//x:fv", nsmgr);

if (answers == null)
    return;

foreach (var item in answers)
{
     var result = ((XmlNode)item).InnerXml;
}