I would like to know what is the easiest way to transform JSON response to GRID view on WPF project.
Asked
Active
Viewed 157 times
1 Answers
0
Since the output of the Newtonsoft.Json.JsonConvert.DeserializeXmlNode is an XmlDocument, I can suggest you the next:
XmlDocument doc = new XmlDocument();
doc.Load(this.FilePath);
var xDocument = XDocument.Load(new XmlNodeReader(doc));
IEnumerable<Employee> elements
= xDocument.Descendants(XName.Get("Employee")).Select(element => new Employee
{
Id = element.Descendants().FirstOrDefault(xElement => xElement.Name == "EmpId").Value,
Name = element.Descendants().FirstOrDefault(xElement => xElement.Name == "Name").Value,
Gender = element.Descendants().FirstOrDefault(xElement => xElement.Name == "Sex").Value,
});
Then you can simple do the next GridViewName.ItemsSource = new ObservableCollection(elements);
Here is the single Employee node:
<Employee>
<EmpId>1</EmpId>
<Name>Sam</Name>
<Sex>Male</Sex>
<Phone Type="Home">423-555-0124</Phone>
<Phone Type="Work">424-555-0545</Phone>
<Address>
<Street>7A Cox Street</Street>
<City>Acampo</City>
<State>CA</State>
<Zip>95220</Zip>
<Country>USA</Country>
</Address>
</Employee>
Here is some useful example.
Regards.