0

Based on the XML below:

<?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>

How can I get the <CfgGroup> element if the attribute value is 103 (referring to agentDBIDs/DBID value, NOT CfgGroup/DBID value)?

Any suggestions?

3 Answers 3

1

You can use SelectNodes() passing the following XPath as parameter to pull out CfgGroup elements where the corresponding agentDBIDs/DBID/@value equals 103, from an XmlDocument :

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

working demo example :

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);
}

Dotnetfiddle Demo

output :

<CfgGroup><DBID value="109" /><tenantDBID value="1" /><name value="group1" /><contractDBID value="0" /></CfgGroup>
<CfgGroup><DBID value="110" /><tenantDBID value="1" /><name value="group2" /><contractDBID value="0" /></CfgGroup>
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @har07, thanks for your solution, it solved my problem.
0

If I understood your question correctly, this should do

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"));

Comments

0

Try this

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");
        }
    }
}
​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.