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. C#
  4. my program works very slow

my program works very slow

Scheduled Pinned Locked Moved C#
xmlhelpquestion
5 Posts 4 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.
  • E Offline
    E Offline
    Erdinc27
    wrote on last edited by
    #1

    hi guys...i get some informations from a site Xml file and convert that file to DataTable and then save it in my form's load..it works well but the problem it works very very slow..when i clicked the button to open my form it takes one min almost to open it..how can i make it faster to work..here is the codes i wrote...

    public XmlNodeList XmlListesi()
    {
    XmlDocument doc = new XmlDocument();
    doc.Load("http://cekilis.millipiyango.gov.tr/haftalik\_loto\_bilgileri.xml");
    XmlElement root = doc.DocumentElement;
    XmlNodeList elemlist = root.GetElementsByTagName("haftalik_loto_bilgileri");
    return elemlist;
    }

        public  DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
        {
            DataTable tablo = new DataTable();
            int TempColumn = 0;
    
            foreach (XmlNode item in xnl.Item(0).ChildNodes)
            {
                TempColumn++;
                DataColumn dc = new DataColumn(item.Name, System.Type.GetType("System.String"));
                if (tablo.Columns.Contains(item.Name))
                    tablo.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
                else
                    tablo.Columns.Add(dc);
            }
    
            int ColumnsCount = tablo.Columns.Count;
            for (int i = 0; i < xnl.Count; i++)
            {
                DataRow dr = tablo.NewRow();
                for (int j = 0; j < ColumnsCount; j++)
                {
                    try
                    {
                        dr\[j\] = xnl.Item(i).ChildNodes\[j\].InnerText;
                    }
                    catch (Exception)
                    { }
                }
                tablo.Rows.Add(dr);
            }
            return tablo;
        }
    
        public  void  InsertSayisal(DataTable dt)
        {
            cmd = dal.InsertSayisal();
            cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.Int);
            cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar);
            cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar);
            cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar);
            cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar);
            cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar);
            cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar);
            cm
    
    L R P 3 Replies Last reply
    0
    • E Erdinc27

      hi guys...i get some informations from a site Xml file and convert that file to DataTable and then save it in my form's load..it works well but the problem it works very very slow..when i clicked the button to open my form it takes one min almost to open it..how can i make it faster to work..here is the codes i wrote...

      public XmlNodeList XmlListesi()
      {
      XmlDocument doc = new XmlDocument();
      doc.Load("http://cekilis.millipiyango.gov.tr/haftalik\_loto\_bilgileri.xml");
      XmlElement root = doc.DocumentElement;
      XmlNodeList elemlist = root.GetElementsByTagName("haftalik_loto_bilgileri");
      return elemlist;
      }

          public  DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
          {
              DataTable tablo = new DataTable();
              int TempColumn = 0;
      
              foreach (XmlNode item in xnl.Item(0).ChildNodes)
              {
                  TempColumn++;
                  DataColumn dc = new DataColumn(item.Name, System.Type.GetType("System.String"));
                  if (tablo.Columns.Contains(item.Name))
                      tablo.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
                  else
                      tablo.Columns.Add(dc);
              }
      
              int ColumnsCount = tablo.Columns.Count;
              for (int i = 0; i < xnl.Count; i++)
              {
                  DataRow dr = tablo.NewRow();
                  for (int j = 0; j < ColumnsCount; j++)
                  {
                      try
                      {
                          dr\[j\] = xnl.Item(i).ChildNodes\[j\].InnerText;
                      }
                      catch (Exception)
                      { }
                  }
                  tablo.Rows.Add(dr);
              }
              return tablo;
          }
      
          public  void  InsertSayisal(DataTable dt)
          {
              cmd = dal.InsertSayisal();
              cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.Int);
              cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar);
              cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar);
              cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar);
              cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar);
              cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar);
              cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar);
              cm
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I don't have the answer, I do have a lot of questions and some suggestions: 1. how many rows and columns are in your DataTable? 2. why are you swallowing exceptions? it is dumb to do so, as it hides potential or real problems you are having. Log them to a log file, and look at it. Don't use MessageBoxes, they are just annoying. Use at most one, at the end of an operation, to say something went wrong and more details are in the log file. 3. why are you storing almost everything as a string? it wastes time and disk space, and it probably hampers functionality too. 4. why are you repeating the same operations all the time? e.g. I see lots of dt.Rows[i] you would better fetch the row once and keep it in a local variable. 5. I can't tell how long dal.InsertSayisal() should take, you should look into that method too. 6. you could use a StopWatch to get timing information, and log that to your log file, together with some extra information; that way you could see how your code behaves, i.e. where exactly time gets wasted. 7. if, I don't know, your DataTable's only purpose is to get the XML data to the database, then maybe you would be better off doing it in a single loop, one row at a time, avoiding the need to keep everything in memory at once. (Depends on #1). :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      1 Reply Last reply
      0
      • E Erdinc27

        hi guys...i get some informations from a site Xml file and convert that file to DataTable and then save it in my form's load..it works well but the problem it works very very slow..when i clicked the button to open my form it takes one min almost to open it..how can i make it faster to work..here is the codes i wrote...

        public XmlNodeList XmlListesi()
        {
        XmlDocument doc = new XmlDocument();
        doc.Load("http://cekilis.millipiyango.gov.tr/haftalik\_loto\_bilgileri.xml");
        XmlElement root = doc.DocumentElement;
        XmlNodeList elemlist = root.GetElementsByTagName("haftalik_loto_bilgileri");
        return elemlist;
        }

            public  DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
            {
                DataTable tablo = new DataTable();
                int TempColumn = 0;
        
                foreach (XmlNode item in xnl.Item(0).ChildNodes)
                {
                    TempColumn++;
                    DataColumn dc = new DataColumn(item.Name, System.Type.GetType("System.String"));
                    if (tablo.Columns.Contains(item.Name))
                        tablo.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
                    else
                        tablo.Columns.Add(dc);
                }
        
                int ColumnsCount = tablo.Columns.Count;
                for (int i = 0; i < xnl.Count; i++)
                {
                    DataRow dr = tablo.NewRow();
                    for (int j = 0; j < ColumnsCount; j++)
                    {
                        try
                        {
                            dr\[j\] = xnl.Item(i).ChildNodes\[j\].InnerText;
                        }
                        catch (Exception)
                        { }
                    }
                    tablo.Rows.Add(dr);
                }
                return tablo;
            }
        
            public  void  InsertSayisal(DataTable dt)
            {
                cmd = dal.InsertSayisal();
                cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.Int);
                cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar);
                cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar);
                cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar);
                cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar);
                cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar);
                cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar);
                cm
        
        R Offline
        R Offline
        RobCroll
        wrote on last edited by
        #3

        Use XmlTextReader instead of XmlDocument. XmlDocument will sit there reading and indexing the whole document before you can start working with it. XmlTextReader will stream the data so you can work while it is reading. Just note XmlTextReader is harder to work with.

        "You get that on the big jobs."

        1 Reply Last reply
        0
        • E Erdinc27

          hi guys...i get some informations from a site Xml file and convert that file to DataTable and then save it in my form's load..it works well but the problem it works very very slow..when i clicked the button to open my form it takes one min almost to open it..how can i make it faster to work..here is the codes i wrote...

          public XmlNodeList XmlListesi()
          {
          XmlDocument doc = new XmlDocument();
          doc.Load("http://cekilis.millipiyango.gov.tr/haftalik\_loto\_bilgileri.xml");
          XmlElement root = doc.DocumentElement;
          XmlNodeList elemlist = root.GetElementsByTagName("haftalik_loto_bilgileri");
          return elemlist;
          }

              public  DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
              {
                  DataTable tablo = new DataTable();
                  int TempColumn = 0;
          
                  foreach (XmlNode item in xnl.Item(0).ChildNodes)
                  {
                      TempColumn++;
                      DataColumn dc = new DataColumn(item.Name, System.Type.GetType("System.String"));
                      if (tablo.Columns.Contains(item.Name))
                          tablo.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
                      else
                          tablo.Columns.Add(dc);
                  }
          
                  int ColumnsCount = tablo.Columns.Count;
                  for (int i = 0; i < xnl.Count; i++)
                  {
                      DataRow dr = tablo.NewRow();
                      for (int j = 0; j < ColumnsCount; j++)
                      {
                          try
                          {
                              dr\[j\] = xnl.Item(i).ChildNodes\[j\].InnerText;
                          }
                          catch (Exception)
                          { }
                      }
                      tablo.Rows.Add(dr);
                  }
                  return tablo;
              }
          
              public  void  InsertSayisal(DataTable dt)
              {
                  cmd = dal.InsertSayisal();
                  cmd.Parameters.AddWithValue("@cek\_no", System.Data.SqlDbType.Int);
                  cmd.Parameters.AddWithValue("@cek\_tarih", System.Data.SqlDbType.NVarChar);
                  cmd.Parameters.AddWithValue("@num1", System.Data.SqlDbType.NVarChar);
                  cmd.Parameters.AddWithValue("@num2", System.Data.SqlDbType.NVarChar);
                  cmd.Parameters.AddWithValue("@num3", System.Data.SqlDbType.NVarChar);
                  cmd.Parameters.AddWithValue("@num4", System.Data.SqlDbType.NVarChar);
                  cmd.Parameters.AddWithValue("@num5", System.Data.SqlDbType.NVarChar);
                  cm
          
          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Ditch the DataTable. Ditch all those ToStrings.

          E 1 Reply Last reply
          0
          • P PIEBALDconsult

            Ditch the DataTable. Ditch all those ToStrings.

            E Offline
            E Offline
            Erdinc27
            wrote on last edited by
            #5

            thanks for your suggestions guys..i will change something in my codes by your suggesstions

            vemedya.com

            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