Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

   // 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 = @"  <ReportTemplate>
      <Page Id='1' LayoutId='1'>
        <Section Id='1'>
          <Body>TEststindfgdfgf</Body>
        </Section>`enter code here`
        <Section Id='2'>
          <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
        <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News' ></Content>
        </Section>
        <Section Id='3'>
           <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
        </Section>
      </Page>
      <Page Id='2' LayoutId='1'>
        <Section Id='1'>
           <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
          <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
        </Section>
        <Section Id='2'>
           <Content Id='9834ebcd0e9537c315a42cf0d8ed32745f89827c' Type='News'></Content>
        </Section>
        <Section Id='3'>
          <Body>dfgdgggf;</Body>
        </Section>
      </Page>
    </ReportTemplate>";


        public class ReportTemplate
        {
              public List<Page> Page { get; set; }
        }
        public class Page
        {
            public string Id { get; set; }
            public string LayoutId { get; set; }
            public List<Section> Section { get; set; }
        }

        public class Section
        {
            public string Id { get; set; }
            public string Body { get; set; }
            public List<Content> Content { get; set; }
        }
        public class Content
        {
            public string Id { get; set; }
            public string Type { get; set; }

        }



var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
var json = JsonConvert.SerializeXmlNode(xmldoc,Newtonsoft.Json.Formatting.Indented, true);
var obj = JsonConvert.DeserializeObject<ReportTemplate>(Regex.Replace(json, "(?<=\")(@)(?!.*\":\\s )", string.Empty, RegexOptions.IgnoreCase));

//This is the error that i am getting

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[LintoXML.Program+Content]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'Page[0].Section[2].Content.Id', line 27, position 17.'

like image 306
user3922960 Avatar asked Jan 03 '23 04:01

user3922960


1 Answers

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);
like image 67
CodingYoshi Avatar answered Jan 14 '23 00:01

CodingYoshi