C# 将数据集转换为XML
我被这个问题困扰了几个小时,似乎无法解决它,所以我在这里问:) 好的,我有这个函数:C# 将数据集转换为XML,c#,asp.net,xml,xml-serialization,dataset,C#,Asp.net,Xml,Xml Serialization,Dataset,我被这个问题困扰了几个小时,似乎无法解决它,所以我在这里问:) 好的,我有这个函数: private void XmlDump() { XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); XElement rootElement = new XElement("dump"); rootElement.Add(TableToX("Support")); string con
private void XmlDump()
{
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
XElement rootElement = new XElement("dump");
rootElement.Add(TableToX("Support"));
string connectionString = ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
string sql = "select * from support";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet("Test");
da.Fill(ds, "support");
// Convert dataset to XML here
var docresult = // Converted XML
Response.Write(docResult);
Response.ContentType = "text/xml; charset=utf-8";
Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
Response.End();
}
我一直在尝试各种不同的方法,但不断出现错误,所以我将如何将数据集转换为XML部分留白
还有一件事,这个查询包含带有特殊字符的列。使用-它将数据集输出为XML。您可以使用
ds.WriteXml
,但这需要有一个流来将输出放入。如果希望输出为字符串,请尝试以下扩展方法:
public static class Extensions
{
public static string ToXml(this DataSet ds)
{
using (var memoryStream = new MemoryStream())
{
using (TextWriter streamWriter = new StreamWriter(memoryStream))
{
var xmlSerializer = new XmlSerializer(typeof(DataSet));
xmlSerializer.Serialize(streamWriter, ds);
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
}
}
}
用法:
var xmlString = ds.ToXml();
// OR
Response.Write(ds.ToXml());
编写如下代码部分
DataTable dt = new DataTable("MyData");
dt.WriteXml(@Application.StartupPath + "\\DataBaseValues.xml");
或者,您也可以直接转换数据集
,正如Oded like所说
private void WriteXmlToFile(DataSet thisDataSet)
{
if (thisDataSet == null)
{
return;
}
// Create a file name to write to.
string filename = "myXmlDoc.xml";
// Create the FileStream to write with.
System.IO.FileStream myFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
// Create an XmlTextWriter with the fileStream.
System.Xml.XmlTextWriter myXmlWriter =
new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
// Write to the file with the WriteXml method.
thisDataSet.WriteXml(myXmlWriter);
myXmlWriter.Close();
}
只需使用Dataset.getXml()
:
我们也可以用这个
Private Function DsToXML(DataSet ds) as System.Xml.XmlDataDocument
Dim xmlDoc As System.Xml.XmlDataDocument
Dim xmlDec As System.Xml.XmlDeclaration
Dim xmlWriter As System.Xml.XmlWriter
xmlWriter = New XmlTextWriter(context.Response.OutputStream,System.Text.Encoding.UTF8)
xmlDoc = New System.Xml.XmlDataDocument(ds)
xmlDoc.DataSet.EnforceConstraints = False
xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
xmlDoc.PrependChild(xmlDec)
xmlDoc.WriteTo(xmlWriter)
Retuen xmlDoc
End Eunction
私有函数DsToXML(数据集ds)作为System.Xml.XmlDataDocument
Dim xmlDoc作为System.Xml.XmlDataDocument
Dim xmlDec As System.Xml.xmlDec声明
Dim xmlWriter As System.Xml.xmlWriter
xmlWriter=newXMLTextWriter(context.Response.OutputStream,System.Text.Encoding.UTF8)
xmlDoc=New System.Xml.XmlDataDocument(ds)
xmlDoc.DataSet.EnforceConstraints=False
xmlDec=xmlDoc.CreateXmlDeclaration(“1.0”,“UTF-8”,无任何内容)
xmlDoc.PrependChild(xmlDec)
xmlDoc.WriteTo(xmlWriter)
复述xmlDoc
结束语
如果ds是您的数据集
您可以使用:
ds.getXml();
这有助于获取XML试试这个。这对我有用
static XmlDocument xdoc = new XmlDocument(); //static; since i had to access this file someother place too
protected void CreateXmlFile(DataSet ds)
{
//ds contains sales details in this code; i.e list of products along with quantity and unit
//You may change attribute acc to your needs ; i.e employee details in the below code
string salemastid = lbl_salemastid.Text;
int i = 0, j=0;
String str = "salemastid:" + salemastid;
DataTable dt = ds.Tables[0];
string xml = "<Orders>" ;
while (j < dt.Rows.Count)
{
int slno = j + 1;
string sl = slno.ToString();
xml += "<SlNo>" + sl +"</SlNo>" +
"<PdtName>" + dt.Rows[j][0].ToString() + "</PdtName>" +
"<Unit>" + dt.Rows[j][1].ToString() + "</Unit>" +
"<Qty>" + dt.Rows[j][2].ToString() + "</Qty>";
j++;
}
xml += "</Orders>";
xdoc.LoadXml(xml);
//Here the xml is prepared and loaded in xml DOM.
xdoc.Save(Server.MapPath("Newsales.xml"));
//You may also use some other names instead of 'Newsales.xml'
//to get a downloadable file use the below code
System.IO.MemoryStream stream = new System.IO.MemoryStream();
XmlTextWriter xwriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
xdoc.WriteTo(xwriter);
xwriter.Flush();
Response.Clear();
Encoding.UTF8.GetString(stream.ToArray());
byte[] byteArray = stream.ToArray();
Response.AppendHeader("Content-Disposition", "filename=OrderRequest.xml");
Response.AppendHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray);
xwriter.Close();
stream.Close();
}
static XmlDocument xdoc=new XmlDocument()//静止的因为我还得去别的地方查这个文件
受保护的void CreateXmlFile(数据集ds)
{
//ds在此代码中包含销售详细信息,即产品列表以及数量和单位
//您可以根据自己的需要更改属性,即以下代码中的员工详细信息
字符串salemastid=lbl_salemastid.Text;
int i=0,j=0;
String str=“salemastid:”+salemastid;
DataTable dt=ds.表[0];
字符串xml=”“;
而(j
是的,这是可行的,但特殊字符显示为问号,有没有办法解决这个问题?这可能是因为ASCII编码。尝试使用编码.UTF8
。现在更新代码ToArray()
您的意思是ToString()
?相似;)。
static XmlDocument xdoc = new XmlDocument(); //static; since i had to access this file someother place too
protected void CreateXmlFile(DataSet ds)
{
//ds contains sales details in this code; i.e list of products along with quantity and unit
//You may change attribute acc to your needs ; i.e employee details in the below code
string salemastid = lbl_salemastid.Text;
int i = 0, j=0;
String str = "salemastid:" + salemastid;
DataTable dt = ds.Tables[0];
string xml = "<Orders>" ;
while (j < dt.Rows.Count)
{
int slno = j + 1;
string sl = slno.ToString();
xml += "<SlNo>" + sl +"</SlNo>" +
"<PdtName>" + dt.Rows[j][0].ToString() + "</PdtName>" +
"<Unit>" + dt.Rows[j][1].ToString() + "</Unit>" +
"<Qty>" + dt.Rows[j][2].ToString() + "</Qty>";
j++;
}
xml += "</Orders>";
xdoc.LoadXml(xml);
//Here the xml is prepared and loaded in xml DOM.
xdoc.Save(Server.MapPath("Newsales.xml"));
//You may also use some other names instead of 'Newsales.xml'
//to get a downloadable file use the below code
System.IO.MemoryStream stream = new System.IO.MemoryStream();
XmlTextWriter xwriter = new XmlTextWriter(stream, System.Text.Encoding.UTF8);
xdoc.WriteTo(xwriter);
xwriter.Flush();
Response.Clear();
Encoding.UTF8.GetString(stream.ToArray());
byte[] byteArray = stream.ToArray();
Response.AppendHeader("Content-Disposition", "filename=OrderRequest.xml");
Response.AppendHeader("Content-Length", byteArray.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray);
xwriter.Close();
stream.Close();
}