Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • XML in ADO.NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 45
    Comment on it

    While working in ADO.NET ActiveX Data Object we can perform manipulation with the text data and the database tables.

     

    But we can also manipulate the data from XML files.

     

     

     

     

    We have DataSet class which is also meant for the XML purpose with ADO.NET.

     

    We call it XML.NET not ADO.NET.

     

    The Read Xml Method

     

    ReadXml is a method that can be used to read a data stream, TextReader, XmlReader, or an XML file and to store into a DataSet object, which can later be used to display the data in a tabular format.

     

      //Create a DataSet object
            DataSet ds = new DataSet();
            // Fill with the data
            ds.ReadXml("books.xml");
    

     

     


    The ReadXmlSchema method

     

    The reads an XML schema from the Dataset object.

     

       DataSet ds = new DataSet();
            ds.ReadSchema(@"c:\books.xml");
    
    
    //Create a dataset object
            DataSet ds = new DataSet("New DataSet");
            // Read xsl in an XmlTextReader
            XmlTextReader myXmlReader = new XmlTextReader(@"c:\books.Xml");
            // Call Read xml schema
            ds.ReadXmlSchema(myXmlReader);
            myXmlReader.Close();
    

     

     

    Writing XML using Data Set

     

    Write Xml Method 

     

    The WriteXml method is used to write XML data in the XML file or in the database.

     

    // Create a DataSet, namespace and Student table
                    // with Name and Address columns
                    DataSet ds = new DataSet("DS");
                    ds.Namespace = "StdNamespace";
                    DataTable stdTable = new DataTable("Student");
                    DataColumn col1 = new DataColumn("Name");
                    DataColumn col2 = new DataColumn("Address");
                    stdTable.Columns.Add(col1);
                    stdTable.Columns.Add(col2);
                    ds.Tables.Add(stdTable);
    
                    //Add student Data to the table
                    DataRow newRow; newRow = stdTable.NewRow();
                    newRow["Name"] = "Mahesh Chand";
                    newRow["Address"] = "Meadowlake Dr, Dtown";
                    stdTable.Rows.Add(newRow);
                    newRow = stdTable.NewRow();
                    newRow["Name"] = "Mike Gold";
                    newRow["Address"] = "NewYork";
                    stdTable.Rows.Add(newRow);
                    newRow = stdTable.NewRow();
                    newRow["Name"] = "Mike Gold";
                    newRow["Address"] = "New York";
                    stdTable.Rows.Add(newRow);
                    ds.AcceptChanges();
    
                    // Create a new StreamWriter
                    // I'll save data in stdData.Xml file
                    System.IO.StreamWriter myStreamWriter = new
                    System.IO.StreamWriter(@"c:\stdData.xml");
    
                    // Writer data to DataSet which actually creates the file
                    ds.WriteXml(myStreamWriter);
                    myStreamWriter.Close();


     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: