Yitzchok / subsonicproject

Automatically exported from code.google.com/p/subsonicproject
0 stars 0 forks source link

Template for static xml deserialization #69

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I just started to use subsonics xml serialization features:
  Product p = new Product(1);   // fetch product with Id 1
  String xml = p.toXML();
which works great.

If I try to load a product from xml, there is no constructor or static
function to do that. I find it quite needless to create a object just to
create a second object:
  Product p = new Product();
  Product p2 = (Product)p.NewFromXML(xml);

I looked at the code and it would be easy to implement it is a static
function in RecordBase.cs. However, for now I decided to add a static
function to the CS_ClassTemplate.aspx
Drop this snippet somewere in the public partial class <%=className%> :
<%=baseClass%> section.
After that you can use:
Product p = Product.LoadFromXML(xml);

#region  XML deserialization

/// <summary>
/// Creates a new <%=className%> instance from the xml
/// specified in the argument
/// </summary>
/// <returns><%=className%>Collection</returns>        
public static <%=className%> LoadFromXML(String xml)
{
   return (<%=className%>)new <%=className%>().NewFromXML(xml);
}

#endregion

Original issue reported on code.google.com by j.steinblock@gmail.com on 23 Apr 2009 at 7:46