0

I'm trying to do the following: load an Xml string into a XDocument object, but when I try to access elements through Descendants method it return nothing when I tried to see the value of inner elements in Visual Studio it does not recognize it as Xml so what is the problem here?

string xml = @"<ArrayOfKeyValueOfstringint xmlns=""http://schemas.microsoft.com/2003/10/Serialization/Arrays"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
  <KeyValueOfstringint>
    <Key>crscmprsn_ttlprt1</Key>
    <Value>1</Value>
  </KeyValueOfstringint>
  <KeyValueOfstringint>
    <Key>ptntmntrfrm_ttlprt1</Key>
    <Value>1</Value>
  </KeyValueOfstringint>
</ArrayOfKeyValueOfstringint>";

var xdoc = XDocument.Parse(xml);
IEnumerable<XElement> elements = xdoc.Descendants("KeyValueOfstringint");

var lst = new List<KeyValuePair<string,int>>();

foreach (var item in elements)
{
    var k = item.Element("Key").Value;
    int v = int.Parse(item.Element("Value").Value);
    var kvp = new KeyValuePair<string,int>(k,v);
    lst.Add(kvp);
}

debugger visualizer

1 Answer 1

2

You need to specify namespace to get your elements:

var ns = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";

var elements = xdoc.Descendants(ns + "KeyValueOfstringint");

For more information about xml namespaces take a look at: Working with XML Namespaces

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.