简体   繁体   中英

How to filter Xml data in XmlDocument

How can I get the CfgGroup/DBID value(s) if my filter value agentDBIDs/DBID value = 103?

<?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="1009" />
      <DBID value="1010" />
      <DBID value="1011" />
      <DBID value="1012" />
      <DBID value="1013" />
      <DBID value="1014" />
      <DBID value="1015" />
      <DBID value="1016" />
      <DBID value="1017" />
      <DBID value="1018" />
      <DBID value="1019" />
      <DBID value="1020" />
    </agentDBIDs>
  </CfgAgentGroup>
</ConfData>

I can filter the agentDBIDs/DBID value, but I have no idea how to get its CfgGroup/DBID value. This is what I've done so far:

XmlNodeList nodeList = AllAgentGroupXML.SelectNodes("/ns:ConfData/ns:CfgAgentGroup/ns:agentDBIDs/ns:DBID", nsMgr); 

foreach (XmlNode abc in nodeList) 
{ 
  if (abc.Attributes["value"].Value.ToString() == "103")
    Console.WriteLine(abc.Attributes["value"].Value.ToString());
}

*Note: ns = namespace, nsMgr = XmlNamespaceManager

Update:

Use the same XML as above, is there any ways to search the specific agentDBID/DBID value and return its CfgGroup/DBID value? How about using the where clause?

Update2: Based on the XML above, how can I get the part of the XML data below if my searching value = 994 (referring to agentDBIDs/DBID value)?

<CfgAgentGroup>
   <CfgGroup>
      <DBID value="109" />
         <tenantDBID value="1" />
         <name value="group1" />
         <contractDBID value="0" />
    </CfgGroup>
      <agentDBIDs>
         <DBID value="103" />
         <DBID value="994" />
      </agentDBIDs>
 </CfgAgentGroup>

Any ideas?

This will give you the CfgGroup/DBID value(s). Use namespace "System.Xml". In this case I have printed CfgGroup/DBID value if it is 103 (agentDBIDs/DBID value).

        string lstr = System.IO.File.ReadAllText(PathOfXML);
        XmlDocument doc = new XmlDocument();

        doc.LoadXml(lstr);


            XmlNodeList CFG_Group_DB_nodeList = doc.SelectNodes(@"/ConfData/CfgAgentGroup/CfgGroup/DBID");

            foreach (XmlNode n1 in CFG_Group_DB_nodeList)
            {

               if (n1.Attributes["value"].Value == "103")
                {
                    Console.WriteLine(n1.Attributes["value"].Value);
                }


            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM