2011-12-05 16 views
13

Zostałem zatrzymany z tym problemem przez kilka godzin i nie wydają się zrozumieć to, więc pytam tutaj :)Konwersja zestawu danych do XML

porządku, mam tej funkcji:

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

Próbowałem wszelkiego rodzaju różnych rzeczy, ale wciąż otrzymuję błędy, więc już opuścił jak konwertować DataSet do części XML pusty.

I kolejna rzecz, zapytanie to zawiera kolumny ze znakami specjalnymi.

Odpowiedz

24

Możesz użyć ds.WriteXml, ale będzie to wymagać od ciebie ustawienia Stream. Jeśli chcesz wyjście na sznurku, spróbuj tej metody rozszerzenia:

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

ZASTOSOWANIE:

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

Tak, to działa, ale znaki specjalne pojawiają się jako znaki zapytania, czy istnieje sposób obejścia tego? – NomenNescio

+0

To prawdopodobnie z powodu kodowania ASCII. Spróbuj z 'Encoding.UTF8'. Aktualizowanie kodu teraz –

+0

Przepraszamy, to powinno być UTF8 –

5

pisać jak poniżej części kodu

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

Albo można przekształcić bezpośrednio the dataSet również jak powiedział Oded jak,

9

Wystarczy użyć Dataset.getXml():

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

Możemy to wykorzystać również

 
    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

jeśli DS to zbiór danych ..

można użyć:

ds.getXml(); 

ten pomaga uzyskać XML

+0

[Powyższa odpowiedź] (http://stackoverflow.com/a/11633050/4519059) jest podobna;). –