在XmlDocument中获取具有特定属性值的xml元素

时间:2015-11-28 15:03:32

标签: c# .net xml xmldocument

基于以下XML:

<?xml version="1.0" encoding="Windows-1252"?>
<ConfData>
   <CfgAgentGroup>
    <CfgGroup>
      <DBID value="109" />
      <tenantDBID value="1" />
      <name value="group1" />
      <contractDBID value="0" />
    </CfgGroup>
    <agentDBIDs>
      <DBID value="103" />
      <DBID value="994" />
    </agentDBIDs>
  </CfgAgentGroup>
  <CfgAgentGroup>
    <CfgGroup>
      <DBID value="110" />
      <tenantDBID value="1" />
      <name value="group2" />     
      <contractDBID value="0" />
    </CfgGroup>
    <agentDBIDs>
      <DBID value="102" />
      <DBID value="103" />         
      <DBID value="1019" />
      <DBID value="1020" />
    </agentDBIDs>
  </CfgAgentGroup>
</ConfData>

如果属性值为103(引用<CfgGroup>值,而不是agentDBIDs/DBID值),如何获取CfgGroup/DBID元素?

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

您可以使用SelectNodes()传递以下XPath作为参数,从CfgGroup:<中提取相应agentDBIDs/DBID/@value等于103的{​​{1}}元素/ p>

XmlDocument

工作演示示例:

/ConfData/CfgAgentGroup[agentDBIDs/DBID/@value=103]/CfgGroup

<强> Dotnetfiddle Demo

输出

var xml = @"<ConfData>
   <CfgAgentGroup>
    <CfgGroup>
      <DBID value='109' />
      <tenantDBID value='1' />
      <name value='group1' />
      <contractDBID value='0' />
    </CfgGroup>
    <agentDBIDs>
      <DBID value='103' />
      <DBID value='994' />
    </agentDBIDs>
  </CfgAgentGroup>
  <CfgAgentGroup>
    <CfgGroup>
      <DBID value='110' />
      <tenantDBID value='1' />
      <name value='group2' />     
      <contractDBID value='0' />
    </CfgGroup>
    <agentDBIDs>
      <DBID value='102' />
      <DBID value='103' />         
      <DBID value='1019' />
      <DBID value='1020' />
    </agentDBIDs>
  </CfgAgentGroup>
</ConfData>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var result = doc.DocumentElement.SelectNodes("/ConfData/CfgAgentGroup[agentDBIDs/DBID/@value=103]/CfgGroup");

foreach(XmlNode r in result)
{
    Console.WriteLine(r.OuterXml);
}

答案 1 :(得分:0)

如果我理解你的问题,那应该

var xDoc = XDocument.Parse(File.ReadAllText(@"C:\YourDirectory\sample.xml"));

var cfgAgentGroups = xDoc.Descendants("CfgAgentGroup");
var cfgGroups = cfgAgentGroups
                .Where(n => n.Element("agentDBIDs")
                                .Elements("DBID")
                                .Any(dbid => dbid.Attribute("value").Value == "103"))
                .Select(cag => cag.Element("CfgGroup"));

答案 2 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement CfgAgentGroup = doc.Descendants("CfgAgentGroup").Where(x => (int)x.Element("agentDBIDs").Element("DBID").Attribute("value") == 103).FirstOrDefault();
            XElement cfgGroup = CfgAgentGroup.Element("CfgGroup");
        }
    }
}
​