gpt4 book ai didi

C# Xml 序列化和反序列化

转载 作者:可可西里 更新时间:2023-11-01 08:43:48 24 4
gpt4 key购买 nike

我正在尝试序列化一个对象并将其保存到 Sql server 2008 xml 字段中。我还有一些反序列化代码可以重新水合对象。我能够序列化对象并将其保存到数据库中,但出现“根元素缺失”异常。

[XmlRoot("Patient")]
public class PatientXml
{
private AddressXml _address = null;
private EmergencyContactXml _emergencyContact = null;
private PersonalXml _personal = null;

[XmlElement]
public PersonalXml Personal
{
get { return _personal; }
set { _personal = value; }
}

[XmlElement]
public AddressXml Address
{
get { return _address; }
set { _address = value; }
}

[XmlElement]
public EmergencyContactXml EmergencyContact
{
get { return _emergencyContact; }
set { _emergencyContact = value; }
}

public PatientXml(){}
public PatientXml(Patient patient)
{
_address = new AddressXml(patient.Address);
_emergencyContact = new EmergencyContactXml(patient.EmergencyInfo);
_personal = new PersonalXml(patient);
}
}

public class PersonalXml
{
private string _firstName = string.Empty, _lastName = string.Empty, _dateOfBirth = string.Empty, _phone = string.Empty;

[XmlAttribute]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}

[XmlAttribute]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}

[XmlAttribute]
public string DateOfBirth
{
get { return _dateOfBirth; }
set { _dateOfBirth = value; }
}

[XmlAttribute]
public string Phone
{
get { return _phone; }
set { _phone = value; }
}

public PersonalXml(){}
public PersonalXml(Patient patient)
{
_firstName = patient.FirstName;
_lastName = patient.LastName;
_dateOfBirth = patient.DateOfBirth.ToShortDateString();
_phone = patient.Phone;
}
}

public class AddressXml
{
private string _address1 = string.Empty, _address2 = string.Empty, _city = string.Empty, _state = string.Empty, _zip = string.Empty;

[XmlAttribute]
public string Address1
{
get { return _address1; }
set { _address1 = value; }
}

[XmlAttribute]
public string Address2
{
get { return _address2; }
set { _address2 = value; }
}

[XmlAttribute]
public string City
{
get { return _city; }
set { _city = value; }
}

[XmlAttribute]
public string State
{
get { return _state; }
set { _state = value; }
}

[XmlAttribute]
public string Zip
{
get { return _zip; }
set { _zip = value; }
}

public AddressXml(){}
public AddressXml(Address address)
{
_address1 = address.Address1;
_address2 = address.Address2;
_city = address.City;
_state = address.State;
_zip = address.ZipCode;
}
}

public class EmergencyContactXml
{
private string _name = string.Empty, _phone = string.Empty, _relationship = string.Empty;

[XmlAttribute]
public string Name
{
get { return _name; }
set { _name = value; }
}

[XmlAttribute]
public string Phone
{
get { return _phone; }
set { _phone = value; }
}

[XmlAttribute]
public string Relationship
{
get { return _relationship; }
set { _relationship = value; }
}

public EmergencyContactXml(){}
public EmergencyContactXml(EmergencyContact contact)
{
_name = contact.ContactName;
_phone = contact.Phone;
_relationship = contact.Relationship;
}
}

序列化的 Xml 输出:

<Patient 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Personal FirstName="Test" LastName="User 1" DateOfBirth="3/13/1966" Phone="6304449866" />
<Address Address1="123 Some St" City="Bartlett" State="CT" Zip="60111" />
<EmergencyContact Name="Dr Chanduwarthana" Phone="6309769484" Relationship="Father" />
</Patient>

序列化和反序列化代码:

public static class XmlSerializer
{
public static string Serialize<T>(T item)
{
MemoryStream memStream = new MemoryStream();
using (XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.Unicode))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
serializer.Serialize(textWriter, item);

memStream = textWriter.BaseStream as MemoryStream;
}
if (memStream != null)
return Encoding.Unicode.GetString(memStream.ToArray());
else
return null;
}

public static T Deserialize<T>(string xmlString)
{
if (string.IsNullOrWhiteSpace(xmlString))
return default(T);

using (MemoryStream memStream = new MemoryStream())
{
using (XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.Unicode))
{
memStream.Position = 0;
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)serializer.Deserialize(memStream);
}
}
}
}

最佳答案

在您的反序列化代码中,您正在创建一个 MemoryStream 和 XmlTextWriter,但您没有为它提供要反序列化的字符串。

using (MemoryStream memStream = new MemoryStream())
{
using (XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.Unicode))
{
// Omitted
}
}

您可以将字节传递给内存流并完全取消 XmlTextWriter。

using (MemoryStream memStream = new MemoryStream(Encoding.Unicode.GetBytes(xmlString)))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)serializer.Deserialize(memStream);
}

关于C# Xml 序列化和反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8722126/

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