none
How to convert xml to datatable in this scenario RRS feed

  • Question

  • I derived Datatable in one class(DataTableEx), then i created one property(TotalRecordCount), then i assigned 5000 to that new property(TotalRecordCount), then i convert this datatable into xml, so it's created.Now i try to convert xml to Datatable, i am not able to get 5000 value to TotalRecordCount Property. so how i can get that values


    Steps to Execute this application
    --------------------------------
    Step 1: Comment Following Line
               dataTable.ReadXml(@"c:\test.xml");        

    Step 2: Execute the applicaiton.
               Now following xml file(test.xml) will be created.

    Step 3: Commont following Lines

                dataTable.TotalRecordCount = 5000;
                dataTable.WriteXml(@"c:\test.xml", System.Data.XmlWriteMode.WriteSchema);

    Step 4: UnComment Following Line
                 dataTable.ReadXml(@"c:\test.xml"); 

    Step 5: Now the xml file will be convert as a Datatable, but now TotalRecordCount is 0 instead of 5000. To achieve this i overwrite follwoing method  "public new void ReadXml(string fileName)" in DatatableEx.xaml.cs.

    now i am struggling to fetch "TotalRecordCount"  property from xml file, how i can achieve it?
    please help me, it's very urgent.

     

     

     

    namespace DataTableExApp
    {
           public class DataTableEx : DataTable
      {
        public DataTableEx()
          : base()
        {
        }
        public DataTableEx(string tableName)
          : base(tableName)
        {
        }
        public DataTableEx(string tableName, string tableNamespace)
          : base(tableName, tableNamespace)
        {
        }
        public DataTableEx(SerializationInfo info, StreamingContext context)
          : base(info, context)
        {
        }
        [XmlElement(ElementName = "TotalRecordCount", DataType = "Int")]
        public int TotalRecordCount
        {
          get;
          set;
        }
        
        
        public new void ReadXml(string fileName)
        {
          base.ReadXml(fileName);
    //      XmlDocument document = new XmlDocument();
     //     document.Load(fileName);          
        }
      }
    }
    
    namespace DataTableExApp
    {
      class Program
      {
        static void Main(string[] args)
        {
          DataTableEx dataTable = new DataTableEx("Test");
          dataTable.TotalRecordCount = 5000;
    
          dataTable.WriteXml(@"c:\test.xml", System.Data.XmlWriteMode.WriteSchema);
          dataTable.ReadXml(@"c:\test.xml");      
        }
      }
    }
    
    • Moved by Rowan Miller Thursday, February 17, 2011 6:42 PM (From:ADO.NET Entity Framework and LINQ to Entities (Pre-Release))
    Thursday, February 10, 2011 4:41 AM

All replies

  • Say the "DataTableEx" class as [Serializable].. In the class declaration.

    Like:

           [Serializable]
    	public class DataTableEx : DataTable

    Then try to Write the Serialized Object to a XML file.. And Deserialize back that XML file in the Object..!


    - <BooM>

    • Edited by BoovendanM Friday, September 16, 2011 8:29 AM In-Context
    Friday, September 16, 2011 8:27 AM