c# - 通过 Newtonsoft.Json.JsonConvert 从同一个类中序列化和反序列化
问题描述
我有问题如何创建一个类定义来序列化和反序列化具有相同结果的对象。
此类定义:
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://hl7.org/fhir")]
public partial class PatientName
{
private PatientNameFamily familyField;
private PatientNameGiven givenField;
/// <remarks/>
public PatientNameFamily family
{
get
{
return this.familyField;
}
set
{
this.familyField = value;
}
}
/// <remarks/>
public PatientNameGiven given
{
get
{
return this.givenField;
}
set
{
this.givenField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://hl7.org/fhir")]
public partial class PatientNameFamily
{
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://hl7.org/fhir")]
public partial class PatientNameGiven
{
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://hl7.org/fhir")]
public partial class PatientNameFamily
{
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://hl7.org/fhir")]
public partial class PatientNameGiven
{
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
序列化正确为 XML,如下所示:
<name>
<family value="Senior"/>
<given value="Sylwester"/>
</name>
但是当尝试反序列化 JSON 时: {\"family\": \"Seniorka\",\"given\": [ \"Sylwia\" ]} ]
我有异常:
将值“Seniorka”转换为类型“PlatformaP1ATD.ZM.PatientNameFamily”时出错,当然我可以将类定义更改为字符串和字符串数组,但我不想让两个类从 JSON 序列化为 xml 和反序列化。
知道如何将字符串值转换为嵌套类型吗?
解决方案
尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
Name name = new Name() { family = new Family() { value = "Senior" }, given = new Given() { value = "Sylwester" } };
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(Name));
serializer.Serialize(writer, name);
}
}
[XmlRoot("name")]
public class Name
{
public Family family { get; set; }
public Given given { get; set; }
}
public class Family
{
[XmlAttribute()]
public string value { get; set; }
}
public class Given
{
[XmlAttribute()]
public string value { get; set; }
}
}
推荐阅读
- python - 使用“积分系统”打印数字的素数
- amazon-lex - 使用 EXPANDED VALUES 的 Amazon Lex 自定义槽类型问题
- express - 开玩笑的承诺没有解决
- wordpress - Cloud Launcher 的 WordPress 中的 .appspot.com 域
- vba - 如何分配箭头键ti图像以在用户周围移动
- postgresql - Kubernetes Citus setup with individual hostname/ip
- c# - 将 ResourceDictionary 与 WPF 中的其他样式一起使用
- python - 在熊猫中,按行值分组column1,然后如何合并column2并在其他colum3上找到最小值?
- linux - 进行系统调用时检测键盘中断的linux选项
- javascript - Vue 路由器不渲染/挂载根路径组件