Converti set di dati in XML

Sono rimasto bloccato con questo problema per alcune ore e non riesco a capirlo, quindi sto chiedendo qui 🙂

Bene, ho questa funzione:

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(); } 

Ho provato tutti i tipi di cose diverse ma continuo a ricevere errori, quindi ho lasciato come convertire DataSet in XML.

E un’altra cosa, questa query contiene colonne con caratteri speciali.

Puoi usare ds.WriteXml , ma questo richiederà di avere un Stream in cui inserire l’output. Se vuoi l’output in una stringa, prova questo metodo di estensione:

 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()); } } } } 

USO:

 var xmlString = ds.ToXml(); // OR Response.Write(ds.ToXml()); 

Basta usare Dataset.getXml() :

 doc.LoadXml(ds.GetXml()); 

Scrivi come parte del codice qui sotto

 DataTable dt = new DataTable("MyData"); dt.WriteXml(@Application.StartupPath + "\\DataBaseValues.xml"); 

Oppure, puoi convertire direttamente il data dataSet anche come detto da Oded,

 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(); } 

Usa DataSet.WriteXml : emetterà il set di dati come XML.

Possiamo usare anche questo

 
   Funzione privata DsToXML (DataSet ds) come System.Xml.XmlDataDocument

     Dim xmlDoc As System.Xml.XmlDataDocument 
     Dim xmlDec As System.Xml.XmlDeclaration
     Dim xmlWriter As System.Xml.XmlWriter
     xmlWriter = Nuovo XmlTextWriter (context.Response.OutputStream, System.Text.Encoding.UTF8)

     xmlDoc = Nuovo System.Xml.XmlDataDocument (ds)
     xmlDoc.DataSet.EnforceConstraints = False
     xmlDec = xmlDoc.CreateXmlDeclaration ("1.0", "UTF-8", Nothing)
     xmlDoc.PrependChild (xmlDec)
     xmlDoc.WriteTo (xmlWriter)
     Retuen xmlDoc
   Fine Eunzione

se ds è il tuo set di dati ..

Puoi usare:

 ds.getXml(); 

questo aiuta a ottenere XML

Prova questo. Ha funzionato per me.

 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; ie list of products along with quantity and unit //You may change attribute acc to your needs ; ie 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 = "" ; while (j < dt.Rows.Count) { int slno = j + 1; string sl = slno.ToString(); xml += "" + sl +"" + "" + dt.Rows[j][0].ToString() + "" + "" + dt.Rows[j][1].ToString() + "" + "" + dt.Rows[j][2].ToString() + ""; j++; } xml += ""; 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(); }