【问题标题】:How to get XmlElement from XmlNodeList in C#?如何从 C# 中的 XmlNodeList 获取 XmlElement?
【发布时间】:2017-01-23 10:32:07
【问题描述】:

以下是我的 XML 文件示例

<?xml version="1.0" encoding="UTF-8"?>
<summary>  
  <testresult>    
    <result value="10" name="long">100</result>
    <result value="12" name="short">200</result>
    <result value="14" name="long">300</result>
  </testresult>
  <testresult>   
    <result value="10" name="short">50</result>
    <result value="12" name="short">60</result>
    <result value="14" name="long">70</result>
  </testresult>
</summary>

我需要获取结果元素的属性值。

我使用 foreach 循环完成了它,如下所示。

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(item.Value);
XmlNodeList nodelist = xmlDoc.SelectNodes("//testresult");

for (int i = 0; i < nodelist.Count; i++)
{
    foreach (XmlElement child in nodelist[i])
    {
        if (child.HasAttributes)
        {
            result.Add(child.Attributes["value"].Value); //This is working fine.
        }       
    }
}

我的最终目标是在 name = "long" only 的情况下识别名称并获得价值。

为此,我需要获取 name Attribute 的值。 我需要这样做不使用 foreach 循环。有什么建议可以在 for 循环中完成我的任务?

谢谢。

【问题讨论】:

标签: c# xml xmldocument


【解决方案1】:

您可以使用 XDocument 轻松解析您的 xml:

var xDoc  = XDocument.Load(@"YourXmlFile");
var result = xDoc.Descendants("result")
              .Where(x=>x.Attribute("name").Value=="long")    
              .Select(x=>x.Value);

【讨论】:

    【解决方案2】:

    如果你可以使用 LINQ to XML,那么你甚至可以得到整数形式的结果

    var xdoc = XDocument.Load(item.Value);
    
    var results = from r in xdoc.Descendants("result")
                  where (string)r.Attribute("name") == "long"
                  select (int)r.Attribute("value");
    

    输出:

    [10, 14, 14]
    

    如果你必须使用 XmlDocument

    var result = from XmlElement tr in xmlDoc.SelectNodes("//testresult")
                 from XmlElement r in tr
                 where r.Attributes["name"].Value == "long"
                 select r.Attributes["value"].Value;
    

    您还可以提供更精确的 XPath

    var result = from XmlElement r in xmlDoc.SelectNodes("//testresult/result[@name='long']")
                 select r.Attributes["value"].Value;    
    

    【讨论】:

      【解决方案3】:

      使用xml linq获取所有值

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Xml;
      using System.Xml.Linq;
      
      namespace ConsoleApplication43
      {
          class Program
          {
              const string FILENAME = @"c:\temp\test.xml";
              static void Main(string[] args)
              {
                  XDocument doc = XDocument.Load(FILENAME);
      
                  var results = doc.Descendants("testresult").Select(x => new {
                      result = x.Elements("result").Select(y => new {
                          value = (int)y.Attribute("value"),
                          name = (string)y.Attribute("name"),
                          text = (int)y
                      }).ToList()
                  }).ToList();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多