如何在XML中读取子类别

时间:2014-10-08 12:43:14

标签: xml c#-4.0 linq-to-xml xelement

我想阅读DivaURI键的值,但我不能这样做。我尝试了很多东西。我想要达到userProperties子类别。最后想要读取DivaURI键的值。

这是一个类别XElement:

<CfgDN>
   <DBID value="6422" />
   <switchDBID value="110" />
   <tenantDBID value="101" />
   <type value="4" />
   <number value="3401" />
   <loginFlag value="1" />
   <registerAll value="2" />
   <groupDBID value="0" />
   <trunks value="0" />
   <routeType value="1" />
   <state value="1" />
   <userProperties>
      <list_pair key="__ROUTER__">
         <str_pair key="BackupVDN" value="3921" />
         <str_pair key="DivaIVRProfile" value="TS_IVR_Profile" />
         <str_pair key="DivaURI" value="I WANT TO READ THIS VALUE!" />
      </list_pair>
      <list_pair key="default">
         <str_pair key="monitor_calls" value="true" />
      </list_pair>
      <list_pair key="TServer">
         <str_pair key="default-dn" value="43921" />
         <str_pair key="router-timeout" value="5" />
      </list_pair>
      <list_pair key="URS_PROD_01">
         <str_pair key="Loaded" value="07/25/2012 01:57 PM" />
         <str_pair key="Loaded by" value="default" />
         <str_pair key="strategy" value="GVP Diva Main Strategy TEST" />
         <str_pair key="strategy0x65" value="GVP Diva Main Strategy TEST" />
      </list_pair>
      <list_pair key="URS_PROD_01_81">
         <str_pair key="Loaded" value="09/15/2012 06:03 AM" />
         <str_pair key="Loaded by" value="default" />
         <str_pair key="strategy" value="GVP Diva Main Strategy PROD" />
         <str_pair key="strategy0x65" value="GVP Diva Main Strategy PROD" />
      </list_pair>
      <list_pair key="URS_PROD_02">
         <str_pair key="Loaded" value="07/25/2012 01:57 PM" />
         <str_pair key="Loaded by" value="default" />
         <str_pair key="strategy" value="GVP Diva Main Strategy TEST" />
         <str_pair key="strategy0x65" value="GVP Diva Main Strategy TEST" />
      </list_pair>
      <list_pair key="URS_PROD_02_81">
         <str_pair key="Loaded" value="09/15/2012 06:03 AM" />
         <str_pair key="Loaded by" value="default" />
         <str_pair key="strategy" value="GVP Diva Main Strategy PROD" />
         <str_pair key="strategy0x65" value="GVP Diva Main Strategy PROD" />
      </list_pair>
   </userProperties>
   <name value="3401" />
   <useOverride value="2" />
   <switchSpecificType value="1" />
   <siteDBID value="0" />
   <contractDBID value="0" />
</CfgDN>

还有一些XElements没有任何userProperties类别:

<CfgDN>
   <DBID value="6409" />
   <switchDBID value="101" />
   <tenantDBID value="101" />
   <type value="19" />
   <number value="3351" />
   <loginFlag value="1" />
   <registerAll value="2" />
   <groupDBID value="0" />
   <trunks value="0" />
   <routeType value="1" />
   <state value="1" />
   <name value="3351_Avaya" />
   <useOverride value="2" />
   <switchSpecificType value="1" />
   <accessNumbers>
      <CfgDNAccessNumber>
         <switchDBID value="110" />
         <number value="41451" />
      </CfgDNAccessNumber>
      <CfgDNAccessNumber>
         <switchDBID value="113" />
         <number value="41451" />
      </CfgDNAccessNumber>
   </accessNumbers>
   <siteDBID value="0" />
   <contractDBID value="0" />
</CfgDN>

我试图像这样联系他们:

IEnumerable<XElement> enumElem = from p in xdoc.Descendants("DivaURI") where p.Parent.Name.Equals("userProperties") select p;
            foreach (XElement child in enumElem)
            {
                //XElement elem = enumElem.Current;
                if (child.Name.LocalName == "DivaURI")
                {
                    ...
                }
            }

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

请注意,"DivaURI"key属性的,而不是元素名称

假设只有一个元素的密钥等于"DivaURI",这是一种可能的方法:

XElement str_pair = 
            xdoc.Descendants("str_pair")
                .FirstOrDefault(o => (string)o.Attribute("key") == "DivaURI");
if(str_pair != null) 
    Console.WriteLine((string)str_pair.Attribute("value"));

更新:

更新了处理默认命名空间和多个<str_pair>元素的代码,如注释中所述:

XNamespace d = "blabla/Configuration/ConfServer/2005/";
IEnumerable<string> result = from p in xdoc.Descendants(d+"str_pair") 
                             where (string)p.Attribute("key") == "DivaURI" 
                             select (string)p.Attribute("value");

答案 1 :(得分:0)

试试这个:

 var getValue = from a in xdoc.Descendants("CfgDN").Elements("userProperties").Elements("list_pair").Elements("str_pair")
                       where a.Attribute("key").Value == "DivaURI"
                       select a.Attribute("value").Value;

输出:

I WANT TO READ THIS VALUE!