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;
}
}
This is solved simply with the
Namespaceproperty onXmlRoot,XmlType,XmlAttributeandXmlElement(etc); example shown below:Output:
Xml (from here):
C#:
Note I’ve only mapped a few of the xml values – but it should show that it essentially works.