通过一个查询将想要的结果得出,然后写入一个xml文件中
导出xml文件的具体格式如下:
<?xml version=”1.0” encoding=”UTF-8”?>
<Document xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation=”采购入库XML Schema-3.0.xsd” Lience=”00120-100209-001”>
<Events version=”3.0”>
<Event name=” PutInStorage”>
<Data bills=”1234567890” name=”云南白药粉” norms=”10g” form=”粉剂” unit=”盒” approval=”国药准字001” manufacturer=”云南白药” indate=”2010-3-5” supplier=”保山市医药有限责任公司” batch=”20100305” validity=”2012-3-1” count=”200” serial=”YNBYF001” />
</Event>
</Events>
</Document>
导出xml文件的具体格式如下:
<?xml version=”1.0” encoding=”UTF-8”?>
<Document xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation=”采购入库XML Schema-3.0.xsd” Lience=”00120-100209-001”>
<Events version=”3.0”>
<Event name=” PutInStorage”>
<Data bills=”1234567890” name=”云南白药粉” norms=”10g” form=”粉剂” unit=”盒” approval=”国药准字001” manufacturer=”云南白药” indate=”2010-3-5” supplier=”保山市医药有限责任公司” batch=”20100305” validity=”2012-3-1” count=”200” serial=”YNBYF001” />
</Event>
</Events>
</Document>
/// 转换成XML格式字符串
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public string DataTableToXml(DataTable dt)
{
StringBuilder strXml = new StringBuilder();
strXml.AppendLine("<XmlTable>");
for (int i = 0; i < dt.Rows.Count; i++)
{
strXml.AppendLine(" <rows>");
for (int j = 0; j < dt.Columns.Count; j++)
{
strXml.AppendLine(" <" + dt.Columns[j].ColumnName + ">" + dt.Rows[i][j] + "</" + dt.Columns[j].ColumnName + ">");
}
strXml.AppendLine(" </rows>");
}
strXml.AppendLine("</XmlTable>");
return strXml.ToString();
}
/// <summary>
/// 生成XML文件
/// </summary>
/// <param name="var">返回的xml字符串</param>
/// <param name="filename">xml文件名</param> /// <returns></returns> public void CreateXml(string val, string filename)
{
UnicodeEncoding ucode = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(Server.MapPath(filename));
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(val);
sw.Close();
sw.Dispose();
}