C#循环遍历xml以获取特定子节点的innertext值

时间:2014-05-24 13:36:16

标签: c# xml

我的XML文档如下所示:

<Runs>
 <Run>
  <LotInfo>
   <Column name="Entity">HST123,</Column> 
   <Column name="Product">XXX123</Column> 
   <Column name="WSOp">1234</Column> 
   <Column name="Route">V234</Column> 
   <Column name="Recipe" /> 
   <Column name="LotNumber">K898722</Column> 
   <Column name="RunStartTime">2014-05-20T17:43:11.8872105</Column> 
  </LotInfo>
  <Operations>
   <Operation type="INTRODUCTION">
   <Column name="Size">1490</Column> 
   <Column name="TimeStamp">2014-05-20T17:43:11.8872105</Column> 
   <Column name="Operator">nismail9</Column> 
   <Column name="Description">
    <![CDATA[ Unknown ]]> 
   </Column>
  <Column name="Status">Success</Column> 
  </Operation>
  <Operation type="RNUCHECK">
   <Column name="Size">1490</Column> 
   <Column name="TimeStamp">2014-05-20T17:43:15.3091731</Column> 
   <Column name="Operator">nismail9</Column> 
   <Column name="Description">
    <![CDATA[ ]]> 
   </Column>
   <Column name="Status">True</Column> 
  </Operation>
  <Operations>
   <Operation type="INTRODUCTION">
   <Column name="Size">1490</Column> 
   <Column name="TimeStamp">2014-05-20T17:58:47.0830259</Column> 
   <Column name="Operator">nismail9</Column> 
   <Column name="Description">
    <![CDATA[ Unknown ]]> 
   </Column>
   <Column name="Status">Success</Column> 
   </Operation>
  </Operations>
 </Run>
</Runs>

我想循环遍历整个XML以获取内部文本值。我需要获取节点<Operation type="INTRODUCTION">的时间戳。每当我得到<Operation type="INTRODUCTION">时,我都会在此节点下获取时间戳:<Column name="TimeStamp">2014-05-20T17:43:11.8872105</Column>,即2014-05-20T17:43:11.8872105

我有一个代码来获取价值,但我不知道如何遍历整个文本以获取所有这些。我只能得到一个。

到目前为止我的代码:

XmlDocument readDoc = new XmlDocument();
            readDoc.Load(fileName);
            int count = readDoc.SelectNodes("/Runs/Run/Operations/Operation[@type='INTRODUCTION']").Count;
            MessageBox.Show(count.ToString());

                        var node = readDoc.SelectSingleNode("/Runs/Run/Operations/Operation[@type='INTRODUCTION']/Column[@name='TimeStamp']");
MessageBox.Show(node.InnerText);

3 个答案:

答案 0 :(得分:0)

您可以使用 SelectNodes 获取所有操作节点的内部文本,然后对其进行迭代以获取内部文本。

var nodes = readDoc.SelectNodes("/Runs/Run/Operations/Operation/
                                    Column[@name='TimeStamp']");
var innerTexts = nodes.OfType<XmlNode>().Select(n => n.InnerText);

确保添加 System.Linq 命名空间以使用可枚举的扩展方法。

答案 1 :(得分:0)

使用XmlDocument.SelectNodes代替XmlDocument.SelectSingleNode来获取所有匹配的节点。您不需要节点的计数,但需要列出它们。获取节点后,使用常规foreach循环结构迭代它们:

    var xmlFile = "c:\\input.xml";
    XmlDocument readDoc = new XmlDocument();
    readDoc.Load(xmlFile);
    var nodes = readDoc.SelectNodes("/Runs/Run/Operations/Operation[@type='INTRODUCTION']/Column[@name='TimeStamp']");
    foreach (XmlElement node in nodes)
    {
        Console.WriteLine(node.InnerText);
    }

输出结果为:

2014-05-20T17:43:11.8872105
2014-05-20T17:58:47.0830259

另一种可能性是使用LINQ2XML来获取所需的节点:

try
{           
    var xmlFile = "c:\\input.xml";
    // load the xml file
    var xml = XDocument.Load(xmlFile);
    // find all operations nodes
    var operations = xml.Root.Descendants("Operations").ToList();
    // iterate over all nodes
    foreach (var operation in operations)
    {
        // find the operation node with type INTRODUCTION
        var op = operation.Elements().Where (o => o.Name == "Operation" && (o.Attribute("type").Value.ToUpper() == "INTRODUCTION")).FirstOrDefault();
        if (op != null)
        {
            // find the timestamp node
            var timestamp = op.Elements().Where (o => o.Name == "Column" && (o.Attribute("name").Value.ToUpper() == "TIMESTAMP")).FirstOrDefault();
            if (timestamp != null)
            {
                // and get the value
                Console.WriteLine(timestamp.Value);
            }
        }
    }
}
catch (Exception exception)
{
    Console.WriteLine(exception.Message);
}

输出与上述相同:

2014-05-20T17:43:11.8872105
2014-05-20T17:58:47.0830259

答案 2 :(得分:0)

使用LINQ to XML

using System.Xml.Linq;

var query = XDocument.Load(xmlPath)
    .Descendants("Operation")
    .Where(w => (string)w.Attribute("type") == "INTRODUCTION")
    .SelectMany(s => s.Elements("Column")
        .Where(w => (string)w.Attribute("name") == "TimeStamp")
        .Select(x => (string)x));