如何将XML反序列化为json,将json反序列化为c#类? //这是我的xml数据,我正在尝试将其映射到.net类。通过将XML转换为json并将json转换回C#类。 字符串xml=@“ TestStIndFGF `在这里输入代码` dfgdgggf; "; 公共类报表模板 { 公共列表页{get;set;} } 公共类页面 { 公共字符串Id{get;set;} 公共字符串LayoutId{get;set;} 公共列表节{get;set;} } 公共课组 { 公共字符串Id{get;set;} 公共字符串体{get;set;} 公共列表内容{get;set;} } 公开课内容 { 公共字符串Id{get;set;} 公共字符串类型{get;set;} } var xmldoc=新的XmlDocument(); LoadXml(xml); var json=JsonConvert.SerializeXmlNode(xmldoc,Newtonsoft.json.Formatting.Indented,true); var obj=JsonConvert.DeserializeObject(Regex.Replace(json)”(?
将XML序列化为json后,复制json并通过引用为其生成类。以下是类:如何将XML反序列化为json,将json反序列化为c#类? //这是我的xml数据,我正在尝试将其映射到.net类。通过将XML转换为json并将json转换回C#类。 字符串xml=@“ TestStIndFGF `在这里输入代码` dfgdgggf; "; 公共类报表模板 { 公共列表页{get;set;} } 公共类页面 { 公共字符串Id{get;set;} 公共字符串LayoutId{get;set;} 公共列表节{get;set;} } 公共课组 { 公共字符串Id{get;set;} 公共字符串体{get;set;} 公共列表内容{get;set;} } 公开课内容 { 公共字符串Id{get;set;} 公共字符串类型{get;set;} } var xmldoc=新的XmlDocument(); LoadXml(xml); var json=JsonConvert.SerializeXmlNode(xmldoc,Newtonsoft.json.Formatting.Indented,true); var obj=JsonConvert.DeserializeObject(Regex.Replace(json)”(?,c#,json,xml,serialization,C#,Json,Xml,Serialization,将XML序列化为json后,复制json并通过引用为其生成类。以下是类: // 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'>
// 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));
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反序列化到这些类中:
// 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));
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; }
}
var xmldoc=new XmlDocument();
LoadXml(xml);
var fromXml=JsonConvert.SerializeXmlNode(xmldoc);
var fromJson=JsonConvert.DeserializeObject(fromXml);
为什么不直接使用XmlSerializer?最后,我需要前端的json数据。