How do Deserialize XML to json and json to c# classes?

后端 未结 1 962
渐次进展
渐次进展 2021-01-21 09:29
   // This is my xml data I am trying to map this to my .net classes. By converting XML to json and json back to C# classes.

       string xml = @\"  

        
相关标签:
1条回答
  • 2021-01-21 10:06

    Once you have serialized the XML into JSON, copy the JSON and generate the classes for it by referring this answer. Here are the classes:

    public class Rootobject
    {
        public Reporttemplate ReportTemplate { get; set; }
    }
    
    public class Reporttemplate
    {
        public Page[] Page { get; set; }
    }
    
    public class Page
    {
        public string Id { get; set; }
        public string LayoutId { get; set; }
        public Section[] Section { get; set; }
        public string text { get; set; }
    }
    
    public class Section
    {
        public string Id { get; set; }
        public string Body { get; set; }
        public object Content { get; set; }
    }
    

    Then deserialize the JSON into those classes:

    var xmldoc = new XmlDocument();
    xmldoc.LoadXml(xml);
    var fromXml = JsonConvert.SerializeXmlNode(xmldoc);
    var fromJson = JsonConvert.DeserializeObject<Rootobject>(fromXml);
    
    0 讨论(0)
提交回复
热议问题