本文整理匯總了C#中System.Data.DataTable.WriteXml方法的典型用法代碼示例。如果您正苦於以下問題:C# DataTable.WriteXml方法的具體用法?C# DataTable.WriteXml怎麽用?C# DataTable.WriteXml使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Data.DataTable
的用法示例。
在下文中一共展示了DataTable.WriteXml方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: exportAccountData
/// <summary>
/// Function to export all the Account data to the XML File
/// </summary>
private static void exportAccountData()
{
DataTable AccountDB = new DataTable();
AccountDB.TableName = "Account Database";
AccountDB.Columns.Add("AccountID");
AccountDB.Columns.Add("Balance");
AccountDB.Columns.Add("Type");
AccountDB.Columns.Add("ClientID");
AccountDB.Columns.Add("Interest");
AccountDB.Columns.Add("Limit");
foreach (KeyValuePair<string, Account> entry in Program.AccountData)
{
DataRow row = AccountDB.NewRow();
row["AccountID"] = entry.Value.ACCOUNTID;
row["Balance"] = entry.Value.BALANCE;
row["Type"] = entry.Value.ACCOUNTTYPE;
row["ClientID"] = entry.Value.CLIENTID;
if (entry.Value.ACCOUNTTYPE=="Interest")
{
AccountInterest temp1 = (AccountInterest)entry.Value;
row["Interest"] = temp1.INTEREST;
row["Limit"] = null;
}
else
{
AccountNoInterest temp2 = (AccountNoInterest)entry.Value;
row["Limit"] = temp2.LIMIT;
row["Interest"] = null;
}
AccountDB.Rows.Add(row);
}
AccountDB.WriteXml("myAccount.xml");
}
示例2: ConvertAltmanDataTableToXml
private static string ConvertAltmanDataTableToXml(DataTable dt)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
writer = new XmlTextWriter(stream, Encoding.Default);
writer.Formatting = Formatting.Indented;
dt.WriteXml(writer);
var count = (int)stream.Length;
var arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
return new UTF8Encoding().GetString(arr);
}
catch
{
return String.Empty;
}
finally
{
if (writer != null)
writer.Close();
}
}
示例3: GetDataTableXml
protected string GetDataTableXml(DataTable dt)
{
dt.TableName = "must have name for xml...";
var writer = new StringWriter();
dt.WriteXml(writer);
return writer.ToString();
}
示例4: ToDataTableXmlString
///// <summary>
///// 序列化物件
///// </summary>
///// <typeparam name="T"></typeparam>
///// <param name="value"></param>
///// <returns></returns>
//public string ObjectToString<T>(T value)
//{
// XmlSerializer ser = new XmlSerializer(value.GetType());
// MemoryStream stream = new MemoryStream();
// ser.Serialize(stream, value);
// stream.Position = 0;
// StreamReader reader = new StreamReader(stream);
// string result = reader.ReadToEnd();
// stream.Dispose();
// reader.Dispose();
// return result;
//}
public string ToDataTableXmlString(IEnumerable value)
{
var type = value.GetType().GetGenericArguments()[0];
DataTable dataTable = new DataTable(type.Name);
PropertyInfo[] Props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
dataTable.Columns.Add(prop.Name);
}
foreach (object item in value)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
using (var memoryStream = new MemoryStream())
{
dataTable.WriteXml(memoryStream);
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
return reader.ReadToEnd();
}
}
}
示例5: Main
static void Main(string[] args)
{
Console.WriteLine("program start!!");
// ClinetsInfoParser.Parse();
var tbl=new DataTable();
tbl.TableName = "custom";
tbl.Columns.Add("Name");
tbl.Columns.Add("Producer");
tbl.Columns.Add("Trash");
tbl.Columns.Add("Expires");
tbl.Columns.Add("Quantity");
tbl.Columns.Add("Trash2");
tbl.Columns.Add("Division");
for (int i = 0; i < 15; i++)
tbl.Rows.Add("Name" + i.ToString(), "Producer" + i.ToString(), i.ToString(), DateTime.Now.ToShortDateString(),
i.ToString(),i.ToString(), "Division" + i.ToString());
var stream=new MemoryStream();
tbl.WriteXml(stream,XmlWriteMode.WriteSchema);
var data= Packer.Pack(stream);
var result = new ConcurrentBag<byte[]>();
result.Add(data);
result.ToQueryResult();
Console.ReadKey();
}
示例6: AddTemplateToXML
private void AddTemplateToXML(string nm)
{
string path = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\templates.xml";
DataTable dt = new DataTable("TemplateTable");
dt.Columns.Add("TemplateName");
DataRow dr;
if (lstTemplate.Items.Count > 0)
{
try
{
for (int b = 0; b < lstTemplate.Items.Count; b++)
{
dr = dt.NewRow();
dr[0] = lstTemplate.Items[b].ToString();
dt.Rows.Add(dr);
}
if (File.Exists(path))
File.Delete(path);
dt.WriteXml(path);
MessageBox.Show("Template successfully inserted.", "CSR Assistant");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "CSR Assistant");
}
}
}
示例7: ToXmlVfp
public void ToXmlVfp(string path)
{
DataTable dt = new DataTable();
dt.TableName = "Tabla1";
DataColumn campo1 = new DataColumn();
campo1.DataType = typeof(string);
campo1.MaxLength = 20;
campo1.ColumnName = "Cae";
DataColumn campo2 = new DataColumn();
campo2.DataType = typeof(string);
campo2.MaxLength = 500;
campo2.ColumnName = "Problema";
dt.Columns.Add(campo1);
dt.Columns.Add(campo2);
foreach (string mensaje in this.Problema)
{
DataRow registro1 = dt.NewRow();
registro1[0] = this.Cae;
registro1[1] = mensaje;
dt.Rows.Add(registro1);
}
dt.WriteXml(path);
}
示例8: SaveResultsToXml
private static void SaveResultsToXml(
string fileName,
DataTable results
)
{
results.WriteXml(fileName);
}
示例9: GetDeliveries
internal static string GetDeliveries()
{
sql = "SELECT " +
"deliveries.id AS delivery_id, purchases.id AS purchase_id, purchases.po_number, deliveries.delivery_date, supplier_id, name AS supplier_name, " +
"lookup_status.description AS status, lookup_status2.description AS completion_status " +
"FROM deliveries " +
"JOIN purchases ON purchases.id = deliveries.purchase_id " +
"JOIN suppliers ON suppliers.id = supplier_id " +
"JOIN lookup_status ON lookup_status.id = deliveries.status " +
"JOIN lookup_status AS lookup_status2 ON lookup_status2.id = deliveries.completion_status " +
"WHERE lookup_status2.description != 'Complete' ";
dt = new DataTable();
dt = Database.Query(sql);
if (dt != null)
{
if (dt.Rows[0].ItemArray[0].ToString() == "-1")
{
result = dt.Rows[0].ItemArray[1].ToString();
}
else
{
StringWriter sw = new StringWriter();
dt.WriteXml(sw);
result = sw.ToString();
}
}
else
{
result = Functions.FormatReturn(0, "No items found");
}
return result;
}
示例10: ConvertBetweenDataTableToXML
public string ConvertBetweenDataTableToXML(DataTable dtNeedCoveret)
{
System.IO.TextWriter tw = new System.IO.StringWriter();
//if TableName is empty, WriteXml() will throw Exception.
dtNeedCoveret.TableName = dtNeedCoveret.TableName.Length == 0 ? "Table" : dtNeedCoveret.TableName;
dtNeedCoveret.WriteXml(tw);
dtNeedCoveret.WriteXmlSchema(tw);
return tw.ToString();
}
示例11: CreateBackUp
public static void CreateBackUp(DataTable dt)
{
try
{
//dt.AcceptChanges();
dt.WriteXml(BackUpFileName);
}
catch(Exception)
{}
}
示例12: Main
static void Main(string[] args)
{
// Создание Таблицы DataTable с именем "Cars"
DataTable tablCars = new DataTable("Cars");
// Создание столбцов
DataColumn carsId = new DataColumn("Id", typeof(int));
DataColumn carsName = new DataColumn("Name", typeof(string));
DataColumn carsCountry = new DataColumn("Country", typeof(string));
DataColumn carsPrice = new DataColumn("Price", typeof(double));
tablCars.Columns.AddRange(new DataColumn[] { carsId, carsName, carsCountry, carsPrice });
// Создание строки с данными
DataRow newRow1 = tablCars.NewRow();
newRow1["Name"] = "BMW"; newRow1["Country"] = "Germany";
newRow1["Price"] = "50000";
tablCars.Rows.Add(newRow1);
DataRow newRow2 = tablCars.NewRow();
newRow2["Name"] = "Audi"; newRow2["Country"] = "Germany";
newRow2["Price"] = "37500";
tablCars.Rows.Add(newRow2);
// Сохранить ТАБЛИЦЫ tablCars в виде XML
tablCars.WriteXml("Cars.xml");
tablCars.WriteXmlSchema("CarsSchema.xsd");
// Создание Таблицы DataTable с именем "Drivers"
DataTable tablDrivers = new DataTable("Drivers");
// Создание столбцов
DataColumn drId = new DataColumn("Id", typeof(int));
DataColumn drName = new DataColumn("Name", typeof(string));
DataColumn drAge = new DataColumn("Age", typeof(string));
tablDrivers.Columns.AddRange(new DataColumn[] { drId, drName, drAge });
// Создание строки с данными
DataRow newRow3 = tablDrivers.NewRow();
newRow3["Name"] = "Ivan"; newRow3["Age"] = "33";
tablDrivers.Rows.Add(newRow3);
DataSet dataSet = new DataSet("AutoPark");
dataSet.Tables.AddRange(new DataTable[] { tablCars, tablDrivers});
// Сохранить DATASET в виде XML
dataSet.WriteXmlSchema("AutoParkSchema.xsd");
dataSet.WriteXml("AutoPark.xml");
// Очистить DataSet.
dataSet.Clear();
Console.WriteLine("XML успешно сформированы!");
Console.ReadKey();
}
示例13: Write
public string Write(TableName tname, DataTable dt)
{
string file = getDataFileName(tname);
using (var writer = NewStreamWriter(file))
{
dt.TableName = tname.Name;
dt.DataSet.DataSetName = tname.DatabaseName.Name;
dt.WriteXml(writer, XmlWriteMode.WriteSchema);
}
return file;
}
示例14: CreateCorrespondingXmlWhenNotExist
public static void CreateCorrespondingXmlWhenNotExist()
{
if (!File.Exists(xmlPath))
{
var dtTableName = new DataTable();
dtTableName.Columns.Add("OriName");
dtTableName.Columns.Add("PascalName");
dtTableName.TableName = "dt";
dtTableName.Rows.Add(new[] { "1", "1" });
dtTableName.WriteXml(xmlPath);
}
}
示例15: CsvToXmlConvert
/*
Example Use
string[] fieldNames = {
"Id",
"Name",
"Age"
};
CsvToXmlConvert("c:\\temp\\example.txt", "TempTable", ";", "c:\\temp\\example.xml", fieldNames);*/
/// <summary>
/// </summary>
/// <param name="inputFile"></param>
/// <param name="dataName"></param>
/// <param name="separator"></param>
/// <param name="outputFile"></param>
/// <param name="fieldnames"></param>
public static void CsvToXmlConvert(
string inputFile,
string dataName,
char separator,
string outputFile,
string[] fieldnames = null)
{
var dt = new DataTable(dataName);
bool firstRow = true;
using (var sr = new StreamReader(inputFile))
{
while (!(sr.EndOfStream))
{
string readLine = sr.ReadLine();
if (readLine == null)
{
continue;
}
string[] fields = readLine.Split(separator);
IEnumerable<string> e = from s in fields
select s;
int c = e.Count();
if (firstRow)
{
for (int ii = 0;
ii <= c - 1;
ii++)
{
string fName = "";
if ((fieldnames == null))
{
fName = "Field" + ii.ToString("000");
}
else
{
fName = fieldnames[ii];
}
dt.Columns.Add(fName);
}
firstRow = false;
}
foreach (string field in fields)
{
dt.Rows.Add(field);
}
}
dt.WriteXml(outputFile);
dt.Dispose();
}
}