Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was able to resolve this. Here are the steps I used: I first created an in-memory run time assembly from xsd using reflection and codedom.compiler with serializable attributes. Using the reflection I created instance of the classes in that assembly and assigned properties from the data I got from database. This class I forwarded to another method that serializes takes an object and serializes it to xml. </p> <p>As far as the mapping, I made sure that the database column names needs to be matched with the xml attribute names and with that I was able map and invoke them.</p> <p>Here is the snippet:</p> <pre><code>object fxClass = myAssembly.CreateInstance(cls.FullName); Type t = fxClass.GetType(); var arrRates = Array.CreateInstance(t, tab.Rows.Count); int i =0; foreach (DataRow dr in tab.Rows) { fxClass = myAssembly.CreateInstance(cls.FullName); PropertyInfo[] fxRateProperties = t.GetProperties(); foreach (PropertyInfo prop in fxRateProperties) { string rowVal = dr[prop.Name].ToString(); if (prop.PropertyType == typeof(DateTime)) { prop.SetValue(fxClass, util.convertToDate(rowVal), null); } else if (prop.PropertyType == typeof(bool)) { prop.SetValue(fxClass, util.convertToBoolean(rowVal), null); } else if (prop.PropertyType == typeof(decimal)) { prop.SetValue(fxClass, util.convertToDecimal(rowVal), null); } else prop.SetValue(fxClass, rowVal, null); } arrRates.SetValue(fxClass,i); i++; } myClass.GetType().GetProperty("ForexRates").SetValue(myClass, arrRates, null); </code></pre> <p>Then pass myClass object to serialize method that accepts object type and that's it</p> <pre><code>public void serializeXML(object portfolio, string xmlPath) { XmlSerializer serial = new XmlSerializer(portfolio.GetType()); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); try { using (FileStream fs = new FileStream(xmlPath, FileMode.Create, FileAccess.Write)) { using (XmlTextWriter tw = new XmlTextWriter(fs, Encoding.UTF8)) { tw.Formatting = Formatting.Indented; serial.Serialize(tw, portfolio, ns); } } } } </code></pre> <p>For this, now I am planning to add a UI piece where the mappings like "ForexRates" are saved in the database and it would then open to any of the xsd types.</p> <p>Thanks very much for you responses.</p>
 

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