简体   繁体   English

如何在XmlDocument中过滤Xml数据

[英]How to filter Xml data in XmlDocument

How can I get the CfgGroup/DBID value(s) if my filter value agentDBIDs/DBID value = 103? 如果我的过滤器值agentDBIDs / DBID值= 103,如何获取CfgGroup / DBID值?

<?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. 我可以过滤agentDBIDs / DBID值,但是我不知道如何获取其CfgGroup / DBID值。 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 *注意:ns =名称空间,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? 使用与上述相同的XML,是否有任何方法可以搜索特定的agentDBID / DBID值并返回其CfgGroup / DBID值? How about using the where clause? 如何使用where子句?

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)? Update2:基于上面的XML,如果我的搜索值= 994(指的是agentDBIDs / DBID值),如何获取下面的XML数据部分?

<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). 这将为您提供CfgGroup / DBID值。 Use namespace "System.Xml". 使用名称空间“ System.Xml”。 In this case I have printed CfgGroup/DBID value if it is 103 (agentDBIDs/DBID value). 在这种情况下,我已经打印了CfgGroup / DBID值(如果它是103)(agentDBIDs / DBID值)。

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


            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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