I am receiving a XML strings from and API, and would like to convert these to C# objects key-value in order to handle these values also apply some asserts.
The messages are of the form string format:
<?xml version="1.0" encoding="UTF-8"?>
<ReportData>
<ProjectName>Test Data</ProjectName>
<Unit>Unit 1</Unit>
<ReportLabel.Cost>394</ReportLabel.Cost>
</ReportData>
After i get the string API response i am trying to deserialize the XMl like this where responseAPI is the xml above:
XmlDocument doc = new XmlDocument();
using (var reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(responseAPI), XmlDictionaryReaderQuotas.Max))
{
XElement xml = XElement.Load(reader);
doc.LoadXml(xml.ToString());
}
At this point i have and xml but i can not get a key-value object.
i am trying
var jsonText = JsonConvert.SerializeXmlNode(doc);
or
serializer = new XmlSerializer(typeof(Object));
using (TextReader reader = new StringReader(responseAPI))
{
var result = (Object)serializer.Deserialize(reader);
}
or
var response = JsonConvert.DeserializeObject<Dictionary<string, object>>(responseAPI);
I am new to with this kind of response in XML and am not sure the way to performing this. It is not as easy as JSON format.