Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Something like this:</p> <pre><code>public class ErrorConverter1 : ITypeConverter&lt;ErrorTableModel, ErrorViewModel&gt; { public ErrorViewModel Convert(ResolutionContext context) { var dbModel = context.SourceValue as ErrorTableModel; if (dbModel == null) return null; var xDocument = XDocument.Parse(dbModel.ErrorXml); var errorElement = xDocument.Descendants(XName.Get("error")).Single(); return new ErrorViewModel() { id = dbModel.ErrorId, application = errorElement.Attribute(XName.Get("application")).Value, type = errorElement.Attribute(XName.Get("type")).Value, message = errorElement.Attribute(XName.Get("message")).Value }; } } </code></pre> <p>Or through XML deserialization. For this you need to change the <code>ErrorViewModel</code> class to:</p> <pre><code>[XmlRoot("error")] public class ErrorViewModel { public int id { get; set; } [XmlAttribute("application")] public string application { get; set; } [XmlAttribute("type")] public string type { get; set; } [XmlAttribute("message")] public string message { get; set; } } public class ErrorConverter2 : ITypeConverter&lt;ErrorTableModel, ErrorViewModel&gt; { public ErrorViewModel Convert(ResolutionContext context) { var dbModel = context.SourceValue as ErrorTableModel; if (dbModel == null) return null; var serializer = new XmlSerializer(typeof(ErrorViewModel)); var result = (ErrorViewModel) serializer.Deserialize(new StringReader(dbModel.ErrorXml)); result.id = dbModel.ErrorId; return result; } } </code></pre> <p>The usage is like this:</p> <pre><code>public class MappingProfile : Profile { protected override void Configure() { Mapper.CreateMap&lt;ErrorTableModel, ErrorViewModel&gt;() .ConvertUsing&lt;ErrorConverter1&gt;()//or use the ErrorConverter2 ; } } class Program { static void Main(string[] args) { Mapper.Initialize(x =&gt; x.AddProfile&lt;MappingProfile&gt;()); var dbModel = new ErrorTableModel() { ErrorId = 1, ErrorXml = "&lt;error application=\"TestApp\" type=\"System.DivideByZeroException\" message=\"Something wicked this way comes.\"&gt;&lt;/error&gt;" }; var viewModel = Mapper.Map&lt;ErrorTableModel, ErrorViewModel&gt;(dbModel); Console.WriteLine(viewModel.id); Console.WriteLine(viewModel.application); Console.WriteLine(viewModel.type); Console.WriteLine(viewModel.message); } } </code></pre> <p>Add guard clauses to your needs.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload