【问题标题】:How do Deserialize XML to json and json to c# classes?如何反序列化 XML 到 json 和 json 到 c# 类?
【发布时间】:2018-05-02 11:49:06
【问题描述】:
   // 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));

//这是我得到的错误

Newtonsoft.Json.JsonSerializationException: '无法反序列化 当前 JSON 对象(例如 {"name":"value"})转换为类型 'System.Collections.Generic.List`1[LintoXML.Program+Content]' 因为 该类型需要一个 JSON 数组(例如 [1,2,3])来反序列化 正确。要修复此错误,请将 JSON 更改为 JSON 数组 (例如 [1,2,3])或更改反序列化类型,使其成为正常的 .NET 类型(例如,不是像整数这样的原始类型,也不是集合 可以从 JSON 反序列化的数组或列表等类型 目的。 JsonObjectAttribute 也可以添加到类型中来强制它 从 JSON 对象反序列化。小路 'Page[0].Section[2].Content.Id',第 27 行,位置 17。'

【问题讨论】:

  • 为什么不简单地使用 XmlSerializer?
  • 最后我的前端需要json数据。

标签: c# json xml serialization


【解决方案1】:

将 XML 序列化为 JSON 后,复制 JSON 并通过引用 this answer 为其生成类。以下是课程:

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; }
}

然后将 JSON 反序列化为这些类:

var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
var fromXml = JsonConvert.SerializeXmlNode(xmldoc);
var fromJson = JsonConvert.DeserializeObject<Rootobject>(fromXml);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多