はXML

2011-12-05 6 views
13

私は数時間のために、この問題で立ち往生してきたし、それを把握することができないようにデータセットを変換するので、私はここで聞いてるのよ:)はXML

さてさて、私はこの機能を持っています:

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

私はさまざまなことを試みてきましたが、エラーが続くので、DataSetをXMLの部分を空白に変換する方法を残しました。

また、このクエリには特殊文字列が含まれています。

答えて

24

ds.WriteXmlを使用できますが、出力するにはStreamが必要です。

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

USAGE:

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

はい、動作しますが、特殊文字は疑問符として表示されますが、回避方法がありますか? – NomenNescio

+0

これはおそらくASCIIエンコーディングのためです。 'Encoding.UTF8'を試してください。今すぐコードを更新する –

+0

申し訳ありません、これはUTF8でなければなりません –

5

書き込みコード部以下のような

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

それとも、あなたが直接変換することができますが、文字列で出力したい場合は、この拡張メソッドを試してみてくださいdataSetも同様に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(); 
} 
9

単にDataset.getXml()を使用します。

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

dsは、データセットであれば我々はまた、

 
    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

をこれを使用することができます。..

あなたが使用することができます。

ds.getXml(); 

これをXMLの取得に役立ちます

+0

[上の回答](http://stackoverflow.com/a/11633050/4519059)も同様です)。 –