I can get the element innertext from expandoobject without any problem. I can't figure out how to get the attribute's value.
By doing Console.WriteLine(obj.Message.Body)
, I can get the expected string inside the body element.
private void TestXML()
{
string xmlString = @"<?xml version=""1.0"" encoding=""utf-8""?><Message important=""yes"" recevied=""2019-2-12""><Body>Hi there fella!</Body></Message>";
XDocument doc = XDocument.Parse(xmlString);
string json = JsonConvert.SerializeXNode(doc);
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json);
Console.WriteLine(obj.Message);
}
I did a debug and and under obj.Message
I can see 3 fields:
- @important with value "yes"
- @received with value "2019-2-12"
- Body with value "Hi there fella!"
Is there a way to retrieve the first 2 fields' values with a @ prefix? I have no idea how to deal with this @ character on dynamic objects.