Now, I've the following code which creates the above described XML as per the XSD.
protected void btnGenerateNewXML_Click(object sender, EventArgs e)
{
try
{
string previousType = string.Empty;
string previousName = string.Empty;
System.Data.DataSet ds = GetBehaviorConfig();
var data = new NewConfiguration.BehCons();
if (ds.Tables.Count > 0)
{
foreach (System.Data.DataTable dt in ds.Tables)
{
foreach (System.Data.DataRow dr in dt.Rows)
{
if (!string.IsNullOrEmpty(dr[0].ToString()))
{
if (previousType != dr[0].ToString() && previousName != dr[1].ToString())
{
previousType = dr[0].ToString();
previousName = dr[1].ToString();
data.Con.Add(new NewConfiguration.BehConsCon
{
Type = dr[0].ToString(),
Name = dr[1].ToString(),
Val = dr[2].ToString()
});
}
}
if (!string.IsNullOrEmpty(dr[3].ToString()))
{//abc
var vbc = new NewConfiguration.BehConsCon();
vbc.ValByCxt.Add(new NewConfiguration.BehConsConValByCxt
{
Cl = dr[3].ToString(),
Cu = dr[4].ToString(),
Ch = dr[5].ToString(),
Value = dr[2].ToString()
});
data.Con.Add((NewConfiguration.BehConsCon)vbc);\\xyz
}
data.Req.Name = dr[6].ToString();
data.Res.Name = dr[7].ToString();
}
}
}
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(NewConfiguration.BehCons));
using (var stream = new System.IO.StreamWriter("C:\\behcfg.xml"))
serializer.Serialize(stream, data);
}
catch (Exception ex)
{
throw;
}
}
I've the following queries
1. I need to generate the above XML exactly the way it is. But the code above has issues which I add the 'vbc' object. It creates multiple <Cons> object and this is not as per my requirement. - Issue where I've commented
\\abc till \\xyz in the above code
2. Is the above code the most effective method, or there can be a better code with lesser validation to achieve the same result?
Satish Chilkury
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
Participant
1926 Points
482 Posts
Effective way of handling XML Serialization
Apr 10, 2013 04:17 AM|SATISD9X|LINK
I've used Xsd2Code to generate the Class and the subsequent Methods for Serialize and Deserialize.
The following is the XSD which was used:
Code Generated by using Xsd2Code for above XSD is:
I've the following result set from DB:
And I want the output XML in the following format for the above DB Resultset :
<?xml version="1.0" encoding="utf-8"?> <Cons> <Con Type="Str" Name="Key"> <Value>Value1</Value> </Con> <Con Type="Bool" Name="Active"> <Value>MainValue</Value> </Con> <Con> <ValByCxt Cl="sur" Cu="en-US" Ch="WEB">newval</ValByCxt> </Con> <Con> <ValByCxt Cl="sur" Cu="en-US" Ch="WEB">anoval</ValByCxt> </Con> <Con> <ValByCxt Cl="cl" Cu="cu" Ch="ch">val</ValByCxt> </Con> <Req Name="Res.sur.ear" /> <Res Name="Req.sur.ear" /> </Cons>
Now, I've the following code which creates the above described XML as per the XSD.
I've the following queries
1. I need to generate the above XML exactly the way it is. But the code above has issues which I add the 'vbc' object. It creates multiple <Cons> object and this is not as per my requirement. - Issue where I've commented \\abc till \\xyz in the above code
2. Is the above code the most effective method, or there can be a better code with lesser validation to achieve the same result?
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
~ Please Mark as Answer if it solves your query ~
Star
9903 Points
1291 Posts
Re: Effective way of handling XML Serialization
Apr 10, 2013 11:10 PM|Pengzhen Song - MSFT|LINK
Hi,
Here is good article, please refer
http://www.c-sharpcorner.com/uploadfile/b81385/best-practices-in-net-xml-serialization-of-complex-classes/
http://www.codeproject.com/Articles/30270/XML-Serialization-of-Complex-NET-Objects
Hope it can help you