0

I need to provide wrapper to my current xml from which i am obtaining the keyvalye pair values. Here is my current code :

string SID_Environment = "SID_" + EnvironmentID.ToString();
XDocument XDoc = XDocument.Load(FilePath_EXPRESS_API_SearchCriteria);
var Dict_SearchIDs = XDoc.Elements().ToDictionary(a => (string)a.Attribute("Name"), a => (string)a.Attribute("Value"));
string Search_ID = Dict_SearchIDs.Where(IDAttribute => IDAttribute.Key == SID_Environment).Select(IDAttribute => IDAttribute.Value).FirstOrDefault();
Console.WriteLine(Search_ID);

Here is my sample xml as follows:

<APIParameters>
     <Parameter Name="SID_STAGE" Value="101198" Required="true"/>
     <Parameter Name="SID_QE" Value="95732" Required="true"/>
 </APIParameters>

Please note that this code is working fine with the sample xml but after modifying my xml with some wrappers i am facing the issue. I need to provide some wrapper to my xml to modify my sample xml as follows:

<DrWatson>
  <Sets>
    <Set>
      <APIParameters>
        <Parameter Name="SID_STAGE" Value="101198" Required="true"/>
        <Parameter Name="SID_QE" Value="95732" Required="true"/>
      </APIParameters>
    </Set>
  </Sets>
</DrWatson>

But when i do it and run my code it throws me an error.Please suggest.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
user2778482
  • 59
  • 1
  • 8
  • [Whats wrong with this solution?](http://stackoverflow.com/questions/18848153/how-can-i-fetch-the-value-from-the-keyvalue-pair-using-c-sharp) – Sergey Berezovskiy Sep 18 '13 at 09:42

2 Answers2

1

XDoc.Elements() returns only the immediate child elements, use Descendants instead.

var parameterElements = xDoc.Descendants("Parameter");
parameterElements.ToDictionary(a => (string)a.Attribute("Name"), 
                               a => (string)a.Attribute("Value"));
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

You need something like:

var apiParams = doc.Descendants("APIParameters");

Then you can modify your code:

var Dict_SearchIDs = apiParams.Elements().ToDictionary(a => (string)a.Attribute("Name"), a => (string)a.Attribute("Value"));
Petr Behenský
  • 620
  • 1
  • 6
  • 17