0

I have an xml which is string. This is part of it.

    <GetModuleInfoResult xmlns =\"http://schemas.datacontract.org/2004/07/GMS.Integration.Service.Contracts.Results\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
  <Code>0</Code>
  <Message i:nil=\"true\"/>
  <Parameters xmlns:a=\"http://schemas.datacontract.org/2004/07/GMS.Integration.Service.Contracts.Entities\">
    <a:Parameter>
      <a:Key>width</a:Key>
      <a:Value>1068</a:Value>
    </a:Parameter>
    <a:Parameter>
      <a:Key>height</a:Key>
      <a:Value>600</a:Value>
    </a:Parameter>

I want to get all the parameter nodes and read their 'key' and 'Value' nodes.

How can I achieve that?

dan mann
  • 113
  • 1
  • 9
  • 1
    Possible duplicate of [How to deal with XML in C#](https://stackoverflow.com/questions/220867/how-to-deal-with-xml-in-c-sharp) – Joe Clay Jan 26 '18 at 15:24
  • refer to the answer in the below link you will get some idea of how to deserialize your xml string https://stackoverflow.com/questions/37350769/xml-deserialization-to-class-error/37351408#37351408 – Maddy Jan 26 '18 at 15:26

1 Answers1

0

Here is one way to grab the key-value pairs from your xml

XDocument doc = XDocument.Load(/* YOUR XML FILE **/);
XNamespace x = "http://schemas.datacontract.org/2004/07/GMS.Integration.Service.Contracts.Entities";
var parameters = doc.Descendants(x + "Parameter").ToDictionary(e => e.Descendants(x + "Key").First().Value, e => e.Descendants(x + "Value").First().Value);
Cinchoo
  • 6,088
  • 2
  • 19
  • 34