I have problem how to create one class definition to serialize and deserialize object with this same result.
This class definition :
/// <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;
}
}
}
serialize correct to XML like below :
<name>
<family value="Senior"/>
<given value="Sylwester"/>
</name>
but when try to deserialize JSON : {\\"family\\": \\"Seniorka\\",\\"given\\": [ \\"Sylwia\\" ]} ]
I have exception :
Error converting value "Seniorka" to type 'PlatformaP1ATD.ZM.PatientNameFamily, of course i can change class definition to string and Array of String , but I wan`t to have two class to serialze to xml ande deserialze from JSON.
Any idea how to convert string value to nested type ?
Try following :
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; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.