Loop in write xml for treeview

I read data from a SQL database in a Web Form application and write that into a XML file and show them using TreeView control. But the problem is that I can read only the Trees with ParentID 0 from database and show them using TreeView. How can I show sub-trees also, having ParentID 1 on the TreeView ? reading from database :

public DataTable GetMenu(int parentId)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection("Data Source =(Local);Initial Catalog=Tajari;Integrated Security=True;"))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from TBLJobs where ParentId=@ParentId", con);
        cmd.Parameters.AddWithValue("@ParentId", parentId);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        con.Close();
        return dt;
    }
}

Creating a XML file code :

private void CreateNodes(int parentId)
{
    DataTable dt = GetMenu(0);
    foreach(DataRow dr in dt.Rows)
    {
        writer.WriteStartElement("Node");
        writer.WriteStartAttribute("Id");
        writer.WriteValue(dr["MenuId"].ToString());
        writer.WriteEndAttribute();

        writer.WriteStartAttribute("Name");
        writer.WriteValue(dr["MenuName"].ToString());
        writer.WriteEndAttribute();

        writer.WriteEndElement();
    }
}

writing to the XML file using XMLWriter

public void CreateMenu()
{
    using (writer = XmlWriter.Create(Server.MapPath("Temp\\Menu.XML")))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("Menu");
        CreateNodes(0);
        writer.WriteEndDocument();
    }
}

Answers


private void CreateNodes(int parentId)
{
    DataTable dt = GetMenu(0);
    foreach(DataRow dr in dt.Rows)
    {
        writer.WriteStartElement("Node");
        writer.WriteStartAttribute("Id");
        writer.WriteValue(dr["MenuId"].ToString());
        writer.WriteEndAttribute();

        writer.WriteStartAttribute("Name");
        writer.WriteValue(dr["MenuName"].ToString());
        writer.WriteEndAttribute();

        **CreateNodes(Convert.ToInt32(dr["MenuId"]));**
        writer.WriteEndElement();
    }
}

Need Your Help

ionic app cannot connect cors enabled server with $http

angularjs http cors ionic-framework asp.net-web-api2

I am trying to build a mobile app with ionic framework. When my application tries to connect server to get json ( server is web api and cors is enabled) it just returns 404 on genymotion and real

Scrolling background - Sprite Kit

ios iphone ios7 xcode5 sprite-kit

So i tried to create an infinite scrolling background by using this post's solution (Sprite kit side scrolling).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.