using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace MyDataTable
{
class Program
{
static void Main(string[] args)
{
DataTable table = new DataTable("myTable");
//
//Definujeme typ
//
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Meno", typeof(string));
table.Columns.Add("Priezvisko", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
//
// Tu pridame jednotlive riadky tabulky
//
table.Rows.Add(0, "Jarko", "Pekny", DateTime.Now);
table.Rows.Add(2, "Peter", "Odkrasy", DateTime.Now);
table.Rows.Add(24, "Nicolas", "Christoff", DateTime.Now);
table.Rows.Add(21, "Janeta", "Vesela", DateTime.Now);
//
//Vytvorime xml subor
//
table.WriteXml("subor.xml");
Console.WriteLine("Subor bol vytvoreny");
}
}
}
Ďalšia možnosť je napr:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace MyDataTable
{
public class User
{
public int Id
{
get;
set;
}
public string Meno
{
get;
set;
}
public string Priezvisko
{
get;
set;
}
public DateTime Date
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
DataTable table2 = new DataTable("myTable");
User myUser = new User();
//
//Definujeme typ
//
table2.Columns.Add("User", typeof(User));
myUser.Id = 0;
myUser.Meno = "Jarko";
myUser.Priezvisko = "Pekny";
myUser.Date = DateTime.Now;
//
// Tu pridame jednotlive riadky tabulky
//
table2.Rows.Add(myUser);
//
//Vytvorime xml subor
//
table2.WriteXml("subor2.xml");
Console.WriteLine("Subor bol vytvoreny");
}
}
}
Výstup : xml súbor
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<myTable>
<User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Id>0</Id>
<Meno>Jarko</Meno>
<Priezvisko>Pekny</Priezvisko>
<Date>2011-11-22T11:11:24.967375+01:00</Date>
</User>
</myTable>
</DocumentElement>
Načítanie xml súboru pomocou DataTable
private void citajXML()
{
try
{
DataTable table2 = new DataTable("myTable");
User myUser = new User();
table2.Columns.Add("User", typeof(User));
//nacitanie xml
table2.ReadXml("subor2.xml");
//prvy zaznam v xml
//pretypujeme na (User)
myUser = (User)table2.Rows[0][0];
Console.WriteLine("Meno : " + myUser.Meno + "Priezvisko : " + myUser.Priezvisko);
Console.ReadKey();
}
catch (Exception ex1)
{
Console.WriteLine(ex1.Message);
}
}
Čítanie XML z URL adresy pomocou DataSet
using System;
using System.Net;
using System.Data;
namespace ReadXML
{
class Program
{
static void Main(string[] args)
{
try
{
string row = String.Empty;
// Create the web request
HttpWebRequest request = WebRequest.Create("http://www.projectik.eu/index.php?option=com_xmap&view=xml&id=1") as HttpWebRequest;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Load data into a dataset
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(response.GetResponseStream());
// Print out all tables and their columns
foreach (DataTable table in myDataSet.Tables)
{
Console.WriteLine("TABLE '{0}'", table.TableName);
Console.WriteLine("Total # of rows: {0}", table.Rows.Count);
Console.WriteLine("--------------------------------------");
//Headers
foreach (DataColumn column in table.Columns)
{
row += "\t" + column.ColumnName +"("+column.DataType.ToString()+")";
}
Console.WriteLine("{0}", row);
//Rows
foreach (DataRow row1 in table.Rows)
{
foreach (string column in row1.ItemArray)
{
row += "\t" + column;
}
Console.WriteLine("{0}", row);
}
Console.WriteLine(System.Environment.NewLine);
}
// Print out table relations
foreach (DataRelation relation in myDataSet.Relations)
{
Console.WriteLine("RELATION: {0}", relation.RelationName);
Console.WriteLine("--------------------------------------");
Console.WriteLine("Parent: {0}", relation.ParentTable.TableName);
Console.WriteLine("Child: {0}", relation.ChildTable.TableName);
Console.WriteLine(System.Environment.NewLine);
}
Console.ReadKey();
}
}
catch (Exception ex1)
{
Console.WriteLine(ex1.Message);
}
}
}
}







