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 6828623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:24:18+00:00 2026-05-26T22:24:18+00:00

I am trying to learn deserialization. I have written this code to deserialize *.hbm.xml

  • 0

I am trying to learn deserialization. I have written this code to deserialize *.hbm.xml files.

Every element is loading correctly but “xmlns”. The message in the exception is:

<hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'> was not expected.

What should be done to solve this?

You want to see my complete code?

Here you go:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping
assembly="Sample.CustomerService.Domain" namespace="Sample.CustomerService.Domain"
>
  <class name="MyTable" table="MyTable" lazy="true" >
    <id name="ID">
      <generator class="identity" />
      <column name="ID" sql-type="int" not-null="true" />
    </id>
    <property name="Name">
      <column name="Name" sql-type="varchar" not-null="false" />
    </property>
    <property name="MfgDate">
      <column name="MfgDate" sql-type="datetime" not-null="true" />
    </property>
  </class>
</hibernate-mapping>
public class Class
    {
        [XmlAttribute("name")]
        public string Name { get; set; }

        [XmlAttribute("table")]
        public string Table { get; set; }

        [XmlAttribute("lazy")]
        public bool Lazy { get; set; }

        [XmlElement("id")]
        public Id Id { get; set; }

        [XmlElement("property")]
        public Property [] Properties { get; set; }
    }

 public class Column
    {
        [XmlAttribute("name")]
        public string ColumnName { get; set; }

        [XmlAttribute("sql-type")]
        public string SqlTypeName { get; set; }

        [XmlAttribute("not-null")]
        public bool NotNull { get; set; }
    }

public class Generator
    {
        [XmlAttribute("class")]
        public string Class { get; set; }
    }

[XmlRoot("hibernate-mapping", Namespace = "urn:nhibernate-mapping-2.2")]
    public class HibernateMapping
    {
        [XmlAttribute("assembly")]
        public string AssemblyName { get; set; }

        [XmlAttribute("namespace")]
        public string NamespaceName { get; set; }

        [XmlElement("class")]
        public Class Class { get; set; }

        public override string  ToString()
        {
            StringBuilder sb = new StringBuilder(NamespaceName);
            sb.Append(".");
            sb.Append(Class.Name);

            return sb.ToString();
        }
    }

public class Id
    {
        [XmlElement("generator")]
        public Generator Generator { get; set; }

        [XmlElement("column")]
        public Column Column { get; set; }
    }

public class Property
    {
        [XmlAttribute("name")]
        public string Name { get; set; }

        [XmlAttribute("column")]
        public string Column { get; set; }

        [XmlAttribute("type")]
        public string SqlTypeName { get; set; }

        [XmlAttribute("not-null")]
        public bool NotNull { get; set; }

        [XmlElement("column")]
        public Column PropColumn { get; set; }

        public string GetColumnName()
        {
            if (PropColumn != null)
            {
                return PropColumn.ColumnName;
            }
            else
            {
                return Name;
            }
        }

        public string GetSqlTypeName()
        {
            if (PropColumn != null)
            {
                return PropColumn.SqlTypeName;
            }
            else
            {
                return SqlTypeName;
            }
        }

        public bool GetNotNull()
        {
            if (PropColumn != null)
            {
                return PropColumn.NotNull;
            }
            else
            {
                return NotNull;
            }
        }
    }

class Program
    {
        static void Main(string[] args)
        {
            //IList<HibernateMapping> list = HbmReader.Get("How_To_Deserialize_a_Hbm_File");
//            string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
//<hibernate-mapping xmlns=""urn:nhibernate-mapping-2.2"">
//    <class name=""Example.Library.Resources.TestObject, Example.Library"" table=""test_object"" lazy=""false"">
//        <id name=""TestId"" column=""TestId"" type=""Guid""> 
//            <generator class=""assigned"" /> 
//        </id> 
//        <property name=""Name"" type=""String"" length=""45"" />
//    </class>
//</hibernate-mapping>";

            Assembly assembly = Assembly.Load("Sample.CustomerService.Domain");
            string[] manifestResourceNames = assembly.GetManifestResourceNames();

            XmlSerializer ser = new XmlSerializer(typeof(HibernateMapping));

            Stream stream = assembly.GetManifestResourceStream(manifestResourceNames[0]);

            HibernateMapping obj = (HibernateMapping)ser.Deserialize(new StreamReader(stream));

            Console.WriteLine(obj.Class.Name);
            Console.WriteLine(obj.Class.Table);
            foreach (var prop in obj.Class.Properties)
            {
                Console.WriteLine("prop: " + prop.Name);
            }

            string str = string.Empty;
        }
    }
  • 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-26T22:24:18+00:00Added an answer on May 26, 2026 at 10:24 pm

    This is solved simply with the Namespace property on XmlRoot, XmlType, XmlAttribute and XmlElement (etc); example shown below:

    Output:

    Example.Library.Resources.TestObject, Example.Library
    test_object
    prop: Name
    

    Xml (from here):

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
        <class name="Example.Library.Resources.TestObject, Example.Library" table="test_object" lazy="false">
            <id name="TestId" column="TestId" type="Guid"> 
                <generator class="assigned" /> 
            </id> 
            <property name="Name" type="String" length="45" />
        </class>
    </hibernate-mapping>
    

    C#:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml.Serialization;
    
    [XmlRoot("hibernate-mapping", Namespace = "urn:nhibernate-mapping-2.2")]
    public class HibernateMapping
    {
        [XmlAttribute("assembly")]
        public string AssemblyName { get; set; }
    
        [XmlElement("class")] // should this be a list?
        public Class Class { get; set; }
    }
    
    public class Class
    {
        [XmlAttribute("name")]
        public string Name { get; set; }
    
        [XmlAttribute("table")]
        public string Table { get; set; }
    
        private readonly List<Property> properties = new List<Property>();
        [XmlElement("property")]
        public List<Property> Properties { get { return properties; } }
    }
    public class Property
    {
        [XmlAttribute("name")]
        public string Name { get; set; }
    }
    static class Program
    {
        static void Main()
        {
            File.WriteAllText("my.xml",
                              @"<?xml version=""1.0"" encoding=""utf-8"" ?>
    <hibernate-mapping xmlns=""urn:nhibernate-mapping-2.2"">
        <class name=""Example.Library.Resources.TestObject, Example.Library"" table=""test_object"" lazy=""false"">
            <id name=""TestId"" column=""TestId"" type=""Guid""> 
                <generator class=""assigned"" /> 
            </id> 
            <property name=""Name"" type=""String"" length=""45"" />
        </class>
    </hibernate-mapping>");
    
    
            var ser = new XmlSerializer(typeof(HibernateMapping));
            var obj = (HibernateMapping)ser.Deserialize(new StreamReader("my.xml"));
            Console.WriteLine(obj.Class.Name);
            Console.WriteLine(obj.Class.Table);
            foreach (var prop in obj.Class.Properties)
            {
                Console.WriteLine("prop: " + prop.Name);
            }
            Console.ReadKey();
        }
    }
    

    Note I’ve only mapped a few of the xml values – but it should show that it essentially works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to learn glib/gtk. I wrote little code which prints files in directory
Trying to learn pointer math better I wrote this code. The intention was to
Trying to learn pointers better I wrote this code. The intention was to print
Trying to learn a bit about PDO and is going through this tutorial .
Trying to learn about php's arrays today. I have a set of arrays like
In trying to learn how to create objects in ActionScript, I have had no
I'm trying to learn C++ so forgive me if this question demonstrates a lack
Trying to learn Django, I closed the shell and am getting this problem now
While trying to learn Unity , I keep seeing the following code for overriding
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or

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.