2011-12-05 60 views
13

Tôi đã gặp sự cố này trong vài giờ và dường như không thể tìm ra được, vì vậy tôi hỏi tại đây :)Chuyển đổi tập dữ liệu thành XML

Được rồi, tôi có chức năng này:

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

Tôi đã thử mọi thứ khác nhau nhưng tôi vẫn gặp lỗi, vì vậy tôi đã để lại cách chuyển đổi DataSet thành phần XML trống.

Và một điều nữa, truy vấn này chứa các cột có ký tự đặc biệt.

Trả lời

24

Bạn có thể sử dụng ds.WriteXml, nhưng điều đó sẽ yêu cầu bạn phải có Stream để đưa đầu ra vào. Nếu bạn muốn đầu ra trong một chuỗi, hãy thử phương pháp mở rộng này:

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

SỬ DỤNG:

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

Có tác dụng này, nhưng các ký tự đặc biệt xuất hiện dưới dạng dấu hỏi, có cách nào xung quanh vấn đề này không? – NomenNescio

+0

Đó có thể là do mã hóa ASCII. Hãy thử với 'Encoding.UTF8'. Đang cập nhật mã ngay bây giờ –

+0

Rất tiếc, đây phải là UTF8 –

5

Viết như mã dưới đây phần

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

Hoặc, bạn có thể chuyển đổi trực tiếp số dataSet cũng như được nói bởi Oded như,

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

Đơn giản chỉ cần sử dụng Dataset.getXml():

doc.LoadXml(ds.GetXml()); 
0

Chúng ta có thể sử dụng điều này cũng

 
    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 
0

nếu ds là bộ dữ liệu của bạn ..

bạn có thể sử dụng:

ds.getXml(); 

này giúp nhận XML

+0

[Câu trả lời ở trên] (http://stackoverflow.com/a/11633050/4519059) tương tự;). –

Các vấn đề liên quan