Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7027043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:11:47+00:00 2026-05-28T00:11:47+00:00

I am trying to serialize an object & save it into a Sql server

  • 0

I am trying to serialize an object & save it into a Sql server 2008 xml field. I also have some deserialization code that re-hydrates the object. I am able to serialize & save the object into the db, but get a “Root element missing” exception.

[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;
    }
}

Serialized Xml output:

<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>

Serization & Deserialization code:

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);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:11:47+00:00Added an answer on May 28, 2026 at 12:11 am

    In your deserialization code you’re creating a MemoryStream and XmlTextWriter but you’re not giving it the string to deserialize.

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

    You can pass the bytes to the memory stream and do away with the XmlTextWriter altogether.

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that I'm trying to serialize into an object. Some
I've run into some problems when trying to serialize my object to XML. The
I'm trying to insert into XML column (SQL SERVER 2008 R2), but the server's
im trying to serialize some object as xml and then read it back the
I am trying to serialize an object to a sql compact database. I am
I'm trying to serialize an object to XML that has a number of properties,
I have been trying to serialize a pretty big object. This object uses dictionaries
I'm trying to figure out a way to serialize some Django model object to
I have run into trouble trying to implement functionality for serializing some classes in
I have a simple object I'm trying to serialize using DataContractSerializer. In my unit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.