0

I want to read the DivaURI key's value but I couldn't do that. I tried lots of things. I want to reach to userProperties child category. And Finally want to read DivaURI key's value.

Here is the one category 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>

There are also XElements that doesn't have any userProperties category:

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

I tried to reach them like this:

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")
                {
                    ...
                }
            }

Any suggestions?

har07
  • 88,338
  • 12
  • 84
  • 137
cihadakt
  • 3,054
  • 11
  • 37
  • 59

2 Answers2

1

Notice that "DivaURI" is value of a key attribute, not an element name.

Assuming there is only one element having key equals "DivaURI", this is one possible way :

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

UPDATE :

Updated code to handle default namespace and multiple <str_pair> elements as stated in comment :

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");
har07
  • 88,338
  • 12
  • 84
  • 137
  • I suspect your XML has default namespace (something like `xmlns="...."`) is that correct? – har07 Oct 08 '14 at 13:25
  • 1
    Try to add this variable before : `XNamespace d = "blabla/Configuration/ConfServer/2005/";`. Then change `Descendants("str_pair")` part to `Descendants(d+"str_pair")` – har07 Oct 08 '14 at 13:29
  • yes! it worked. But I guess it shows the first or default occurence in xdoc. How can I get the list of the other DivaURI values? – cihadakt Oct 08 '14 at 13:34
  • @cihata87 check **UPDATE** section – har07 Oct 08 '14 at 13:38
0

Try this :

 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;

Output:

I WANT TO READ THIS VALUE!

Thirisangu Ramanathan
  • 614
  • 1
  • 11
  • 20