gpt4 book ai didi

c# - 如何将 XML 反序列化为 json 和将 json 反序列化为 c# 类?

转载 作者:太空宇宙 更新时间:2023-11-03 18:55:06 24 4
gpt4 key购买 nike

   // 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: '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.'

最佳答案

将 XML 序列化为 JSON 后,复制 JSON 并通过引用 this answer 为其生成类.以下是类(class):

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

关于c# - 如何将 XML 反序列化为 json 和将 json 反序列化为 c# 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47372027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com