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

  • Home
  • SEARCH
  • 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 3281722
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:50:50+00:00 2026-05-17T19:50:50+00:00

I have a SQL Server database that contains an xml column. I need to

  • 0

I have a SQL Server database that contains an xml column. I need to map that xml column to an expando object within my domain entity. I am using NHibernate. How do I extend NHibernate to accommodate this? I am assuming (I am new to NHibernate) that I have to override the implementation to get and set the xml data, but I don’t know how to do that in NHibernate.

  • 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-17T19:50:50+00:00Added an answer on May 17, 2026 at 7:50 pm

    Thanks to Petr’s answer, I have come up with the following initial attempt at a user type to handle the expando. It works quite nicely and I can now offer client set properties for each of my objects. I set the properties that each object must have, and each client can then add their own properties to meet their needs.

    One caveat – I am only setting this up for persistence purposes. Searching is not necessary in this app as all querying is done against a MongoDB database that has denormalized copies of the data.

    public class ExpandoUserType : IUserType
    {
        public object Assemble(object cached, object owner)
        {
            return cached;
        }
    
        public object DeepCopy(object value)
        {
            return value;
        }
    
        public object Disassemble(object value)
        {
            return value;
        }
    
        public bool Equals(object x, object y)
        {
            return false;
        }
    
        public int GetHashCode(object x)
        {
            return 0;
        }
    
        public bool IsMutable
        {
            get { return false; }
        }
    
        public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
        {
            var obj = NHibernateUtil.XmlDoc.NullSafeGet(rs, names[0]);
    
            if (obj == null) return null;
    
            var xmldoc = (XmlDocument)obj;
    
            dynamic expando = new ExpandoObject();
    
            foreach (XmlElement el in xmldoc.FirstChild.ChildNodes)
            {
                object val = null;
    
                switch (Convert.ToString(el.Attributes["type"].InnerText).ToLower())
                {
                    case "string":
                        val = el.InnerText;
                        break;
    
                    case "int32":
                        val = Convert.ToInt32(el.InnerText);
                        break;
    
                    case "int16":
                        val = Convert.ToInt16(el.InnerText);
                        break;
    
                    case "int64":
                        val = Convert.ToInt64(el.InnerText);
                        break;
    
                    case "bool":
                        val = Convert.ToBoolean(el.InnerText);
                        break;
    
                    case "datetime":
                        val = Convert.ToDateTime(el.InnerText);
                        break;
    
                    case "byte":
                        val = Convert.ToByte(el.InnerText);
                        break;
    
                    case "decimal":
                        val = Convert.ToDecimal(el.InnerText);
                        break;
                }
                ((IDictionary<String, Object>)expando).Add(el.Name, val);
            }
    
            return expando;
    
        }
    
        /// <summary>
        /// Transforms the expando object to an XML Document for storage in SQL.
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="value"></param>
        /// <param name="index"></param>
        public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
        {
            if (value == null || value == DBNull.Value)
            {
                NHibernateUtil.String.NullSafeSet(cmd, null, index);
            }
            else
            {
                NHibernateUtil.XmlDoc.Set(cmd, expandoToXML((ExpandoObject) value, "root"), index);
            }
        }
    
        public object Replace(object original, object target, object owner)
        {
            return original;
        }
    
        public Type ReturnedType
        {
            get { return typeof(ExpandoObject); }
        }
    
        public NHibernate.SqlTypes.SqlType[] SqlTypes
        {
            get { return new[] { NHibernateUtil.XmlDoc.SqlType };  }
        }
    
    
        private static XmlDocument expandoToXML(dynamic node, String nodeName)
        {
            XElement xmlNode = new XElement(nodeName);
    
            foreach (var property in (IDictionary<String, Object>)node)
            {
    
                if (property.Value.GetType() == typeof(ExpandoObject))
                    xmlNode.Add(expandoToXML(property.Value, property.Key));
    
                else
                    if (property.Value.GetType() == typeof(List<dynamic>))
                        foreach (var element in (List<dynamic>)property.Value)
                            xmlNode.Add(expandoToXML(element, property.Key));
                    else
                    {
                        XElement xnode = new XElement(property.Key, property.Value);
                        xnode.SetAttributeValue("type", property.Value.GetType().Name);
                        xmlNode.Add(xnode);
                    }
            }
    
            return xmlNode.GetXmlNode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VARCHAR column in a SQL Server 2000 database that can contain
I have a SQL Server 2008 database that contains DateTimeOffset objects. As per this
I have a table in a SQL Server database that contains a row I
I have a SQL Server 2008 database (call it productionDB) that contains data and
I have an SQL server database that I am querying and I only want
I have a SQL Server 2005 database that is suffering from lock starvation because
I have a SQL Server 2005 database that I'm trying to access as a
I have a SQL Server 2005 database that is linked to an Oracle database.
We have a Microsoft SQL Server 2005 database that needs to be converted back
I have a SQL Server 2000 database instance that is rarely updated. I also

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.