This is probably quite noob, but I am having problems with something that is most lightly quite simple. This is my xml:
<CustomFieldOptions>
<DataType>Text</DataType>
<Options>
<Option Key="Advokater">category_advokater</Option>
<Option Key="Arkitektur- & ingeniørvirksomheder">category_arkitektur</Option>
<Option Key="Bank & finans">category_bank</Option>
</Options>
I am trying to get the node value and the key attribute to a list, like:
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
while(reader.Read())
{
reader.ReadToFollowing("Option");
key = reader.ReadInnerXml();
reader.MoveToFirstAttribute();
value = reader.Value;
if(key.Length > 0 && value.Length > 0)
categoryList.Add(key, value);
}
}
So, for the first Option I should get a Key-Value pair of <"category_advokater","Advokater">
But when adding to list I have mixed values of current/previous lines. Where am I getting wrong?
Thx in advance!
/snedker
You can do that using LINQ to XML
:
var xmlDocument = XDocument.Load("path");
var options = xmlDocument.Descendants("Option")
.ToDictionary(x => (string)x, x => (string)x.Attribute("Key"));