通过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",我当然可以将类定义更改为字符串和字符串数组,但我不希望有两个类串行化为xml和从JSON中取消序列化。

知道如何将字符串值转换为嵌套类型吗?

尝试以下操作:

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:temptest.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; }
}

}

最新更新