C# algorithm building a XML Tree / Hierarchy.


S

schtroumfps

Hi, I am working on a way to fill a homemade tree component with a XML
file a this moment and I have a lot of questionning about the
limitation of my C# code. I would like to have some advises or a
better way to deal with that limitation of that algorithm.
Here is a resume of what I am working on : I am developping a server-
side data component in C# and MS-SQL stored procedure. The component
is filling a javascript tree (treeview look) component with an XML
file on the client side through a ASHX file. My first idea was to do a
function with 2 nested loop (1 one for the parent 1 for each child)
but due to some performance problem I have changed my mind.
My big questionning is that I only have 2 levels (parents and child)
and I would like to add 2 more (parents, child, sub-child, sub-sub-
child)...and maybe later a couple more. So at this moment the thing is
working fine but I am limited to that level of complexity. I have no
control over the javascript tree component and naturally, I have to
passed the exact format to the application to get the result on
screen.

Actually, I know that my problem reside on the algorithm but I am
trying to find out a better way to deal with the problem. I would like
to build a recursive tree but I do not know where to start. Anybody
have an idea? I hope I am clear enough.


Sample of the code:

public string GetTreeResultsInDB(int PARAM_ID_1, int PARAM_ID_2, int
PARAM_ID_3)
{
SqlDataReader rdr = null;

try
{
SqlParameter[] signOnParms;

signOnParms = new SqlParameter[]
{
new SqlParameter(PARAM_ID_1,SqlDbType.Int),
};

signOnParms[0].Value = PARAM_ID_1;

rdr = SqlHelper.ExecuteReader(SqlHelper.CONN_STRING,
CommandType.StoredProcedure, SHOW_ONE_BU, signOnParms);

SqlDataReader rdr2 = null;

/// <summary>
/// Construct a XML that's going to be pass to the
ashx page.
/// </summary>
StringBuilder xml = new StringBuilder("");

xml.Append("<?xml version=" + "\"" + "1.0" + "\"" + "
encoding=" + "\"" + "UTF-8" + "\"" + "?>");
xml.Append("<rows>");

int answer = 0;
int counter = 1;
int Parent2 = 0;
int Parent1 = 0;

while (rdr.Read())
{
if (counter == 1)
{
SqlParameter[] signOnParms2;

signOnParms2 = new SqlParameter[]
{
new
SqlParameter(PARAM_ID_1,SqlDbType.Int),
new
SqlParameter(PARAM_ID_2,SqlDbType.TinyInt),
new
SqlParameter(PARAM_ID_3,SqlDbType.Int),
};

signOnParms2[0].Value = ID_1;
signOnParms2[1].Value = ID_2;
signOnParms2[2].Value = ID_3;


/// <summary>
/// Get the specific BU. BU parameter is
passed
/// to that function when clicking the
specific BU on the first page.
/// </summary>
rdr2 =
SqlHelper.ExecuteReader(SqlHelper.CONN_STRING,
CommandType.StoredProcedure, SHOW_SUMMARIZED_TREE_RESULTS,
signOnParms2);
}

string Bu = rdr.GetString(0);

/// <summary>
/// Get rid of the space in business unit to
construct XML row ID.
/// </summary>
Bu = Bu.Replace(" ", "");
xml.Append("<row id=" + @"""" + Bu + @"""" + "
open=\"1\">");
xml.Append("<userdata name=\"tip_0\"><![CDATA[" +
@"""" + Bu + @"""" + "]]></userdata>");
xml.Append("<cell image=" + @"""" + IMAGE_FOLDER +
@"""" + ">" + rdr.GetString(0) + "</cell>");


int counterChildren = 1;
while (rdr2.Read())
{
Parent1 = Convert.ToInt32((rdr2["index1"]));

//Parent
if (rdr2.GetValue(1).ToString().Length == 0)
{
// Compare the previous parent with
// the current parent. Include a closed
row tag
// before adding a new parent if the
descendance
// of the previous parent is finished.
byte AP_icon = rdr2.IsDBNull(8) ?
Convert.ToByte(0) : rdr2.GetByte(8);
xml.Append("<row id=" + @"""" +
rdr2.GetValue(4) + "." + rdr2.GetInt32(19).ToString() + @"""" + " >");
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top