Get Dictionary From XML

Niladri.Biswas
Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 781
public static Dictionary<string, string> GetDictionaryFromXML(string inputXml)
{
string elementName = string.Empty;
Dictionary<string, string> responseDictionary = new Dictionary<string, string>();
using (XmlReader xmlReader = XmlReader.Create(new StringReader(inputXml.Trim())))
{
while (xmlReader.Read())
{
if (xmlReader.NodeType == XmlNodeType.Element) elementName = xmlReader.LocalName;

if (xmlReader.NodeType == XmlNodeType.CDATA || xmlReader.NodeType == XmlNodeType.Text)
responseDictionary[elementName] = xmlReader.Value;

else if (xmlReader.HasValue)
responseDictionary[xmlReader.LocalName] = xmlReader.Value;
}
}
return responseDictionary;
}

Comments or Responses

Login to post response