Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. How to generate XML File from table

How to generate XML File from table

Scheduled Pinned Locked Moved XML / XSL
databasetutorialcsharpasp-netxml
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abdullah S Abdelhay
    wrote on last edited by
    #1

    I have a table in database :

    CREATE TABLE [LeftMenu] (
    [NodeID] [int] NOT NULL ,
    [ParentNodeID] [int] NULL ,
    [FK_LangID] [int] NULL ,
    [Text] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
    [lastlevel] [bit] NULL ,
    [linkLevel] [bit] NULL ,
    CONSTRAINT [PK_LeftMenu] PRIMARY KEY CLUSTERED
    (
    [NodeID]
    ) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GO

    and i want to generate XML file from this table with relation between 'NodeID' column and 'ParentNodeID' to fill ASP.net 2 Menu Control or TreeView Control like this example:

    <menu>
    <sub NodeID="1" ParentNodeID="" Text="aaa" />
    <sub NodeID="2" ParentNodeID="" Text="aaa" >
    <sub NodeID="3" ParentNodeID="2" Text="aaa" >
    <sub NodeID="20" ParentNodeID="3" Text="aaa" />
    <sub NodeID="20" ParentNodeID="3" Text="aaa" />
    </sub>
    <sub NodeID="10" ParentNodeID="2" Text="aaa" />
    <sub NodeID="12" ParentNodeID="2" Text="aaa" />
    </sub>
    <sub NodeID="4" ParentNodeID="" Text="aaa" />
    <sub NodeID="5" ParentNodeID="" Text="aaa" />
    <sub NodeID="6" ParentNodeID="" Text="aaa">
    <sub NodeID="14" ParentNodeID="6" Text="aaa" />
    </sub>
    <sub NodeID="7" ParentNodeID="" Text="aaa" />
    <sub NodeID="8" ParentNodeID="" Text="aaa" />
    <sub NodeID="9" ParentNodeID="" Text="aaa" />
    </menu>

    plz reply me. Thank you Abdullah Abdelhay

    T 1 Reply Last reply
    0
    • A Abdullah S Abdelhay

      I have a table in database :

      CREATE TABLE [LeftMenu] (
      [NodeID] [int] NOT NULL ,
      [ParentNodeID] [int] NULL ,
      [FK_LangID] [int] NULL ,
      [Text] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [lastlevel] [bit] NULL ,
      [linkLevel] [bit] NULL ,
      CONSTRAINT [PK_LeftMenu] PRIMARY KEY CLUSTERED
      (
      [NodeID]
      ) ON [PRIMARY]
      ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
      GO

      and i want to generate XML file from this table with relation between 'NodeID' column and 'ParentNodeID' to fill ASP.net 2 Menu Control or TreeView Control like this example:

      <menu>
      <sub NodeID="1" ParentNodeID="" Text="aaa" />
      <sub NodeID="2" ParentNodeID="" Text="aaa" >
      <sub NodeID="3" ParentNodeID="2" Text="aaa" >
      <sub NodeID="20" ParentNodeID="3" Text="aaa" />
      <sub NodeID="20" ParentNodeID="3" Text="aaa" />
      </sub>
      <sub NodeID="10" ParentNodeID="2" Text="aaa" />
      <sub NodeID="12" ParentNodeID="2" Text="aaa" />
      </sub>
      <sub NodeID="4" ParentNodeID="" Text="aaa" />
      <sub NodeID="5" ParentNodeID="" Text="aaa" />
      <sub NodeID="6" ParentNodeID="" Text="aaa">
      <sub NodeID="14" ParentNodeID="6" Text="aaa" />
      </sub>
      <sub NodeID="7" ParentNodeID="" Text="aaa" />
      <sub NodeID="8" ParentNodeID="" Text="aaa" />
      <sub NodeID="9" ParentNodeID="" Text="aaa" />
      </menu>

      plz reply me. Thank you Abdullah Abdelhay

      T Offline
      T Offline
      tptshepo
      wrote on last edited by
      #2

      HI Run this in a console app and change the connection string!

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Data;
      using System.Data.SqlClient;

      namespace MenuGen
      {
      class Program
      {
      public static string Crlf = System.Environment.NewLine;

          private class LeftMenuNode
          {
              public int NodeID = 0;
              public string ParentNodeID = string.Empty;
              public int FK\_LangID = 0;
              public string Text = string.Empty;
              public bool LastLevel = false;
              public bool LinkLevel = false;
      
              public List SubMenus;
      
              public LeftMenuNode()
              {
                  SubMenus = new List();
              }
          }
      
          //get the menu data from the database
          static void GetDBMenus(ref DataSet ds)
          {
              string sql = "SELECT \* FROM LeftMenu";
              SqlCommand cmd = new SqlCommand(sql, new SqlConnection(@"data source=\*\*\*;initial catalog=\*\*\*;Integrated Security=True"));
              cmd.CommandType = CommandType.Text;
              SqlDataAdapter adapter = new SqlDataAdapter(cmd);
              ds.Clear();
              using (adapter)
              {
                  adapter.Fill(ds);
              }
          }
      
          static void Main(string\[\] args)
          {
              DataSet dsMenus = new DataSet("Menu");
              //get the list of menus
              GetDBMenus(ref dsMenus);
      
              //make sure we have a table to work with
              if (dsMenus.Tables.Count == 0)
                  return;
      
              //kick off the menu gen process
              BuilMenuXML(ref dsMenus);
      
              //now that we've buld a class structure we can work with
              //all we have to do now if
          }
      
          static void BuilMenuXML(ref DataSet ds)
          {
              int tab = 0;
              string xml = string.Empty;
      
              xml = "
      

      "
      + Crlf;

              //populate our custom menu class
              List menus = new List();
              foreach (DataRowView row in ds.Tables\[0\].DefaultView)
              {
                  LeftMenuNode menuItem = new LeftMenuNode();
                  menuItem.NodeID = Convert.ToInt32(row\["NodeID"\]);
                  menuItem.ParentNodeID = Convert.ToString(row\["ParentNodeID"\] == DBNull.Value ? "" : row\["ParentNodeID"\]);
                  menuItem.Text = Convert.ToString(row\["Text"\]);
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups