I have this xml file :
<key1>value 1</key1>
<key2>value 2</key2>
<key3>value 3</key3>
<key4>value 4</key4>
I would like to parse it and get a dictionnary containing all the attributes of my element. To do that I have this code :
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
string key="";
string value="";
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
key=(reader.Name)
break;
case XmlNodeType.Text:
value=(reader.Value)
element_dictionnary.Add(key,value);
break;
}
}
}
Everything is working fine, the problem is if one node is empty like with this file :
<key1></key1>
<key2>value 2</key2>
<key3>value 3</key3>
<key4>value 4</key4>
Everything is shifted and my dictionnary becomes this :
key1/value2
key2/value3
.....
So basicaly, I would like to know how it's possible to associate the node value to the node name, even if the node value is empty.
Thanks in advance for your help.
Just check that reader.Value is empty using
So your code become