2012-02-03 6 views
0

私は現在作業しているプロジェクトに強く型付けされたデータセットを持っており、DataSetからDataRowオブジェクト(DataSetのDataTableは1つだけ)をXML文字列。私は完全な失敗だけで次のように試みました:DataSet.DataTable.DataRow(Single)to XML String

string originalXmlString = string.Empty; 

DataSet ds = new DataSet(); 

ds.Tables.Add(this.ObjectDataRow.Table); 
ds.Tables[0].ImportRow(this.ObjectDataRow); 

using (StringWriter sw = new StringWriter()) 
{ 
    ds.Tables[0].WriteXml(sw);       
    originalXmlString = sw.ToString(); 
} 

req.OriginalDataRow = originalXmlString; 

何か助けていただければ幸いです!特にあなたが取得しているか間違っている何が起こっているのか、エラーで何

おかげで、 キース

答えて

2

私はClone()機能に関するMSDNページの助けを借りてそれを理解することができました。

次のコードは、改訂され、素晴らしい作品れる:

string originalXmlString = string.Empty; 

DataSet ds = new DataSet(); 

//ds.Tables.Add(this.ObjectDataRow.Table); 

ds.Tables.Add(this.ObjectDataRow.Table.Clone()); 

ds.Tables[0].ImportRow(this.ObjectDataRow); 

using (StringWriter sw = new StringWriter()) 
{ 
    ds.Tables[0].WriteXml(sw);       
    originalXmlString = sw.ToString(); 
} 

req.OriginalDataRow = originalXmlString; 
0

? コードからは、1行のDataTableのXMLバージョンが必要です。

+0

あなたの答えは、おそらくコメントでなければなりません。 –

+0

上記のコードでは、テーブル(this.ObjectDataRow.Table)が別のDataSetに属することを示すエラーが表示されます。そこで、コードを変更して手動でテーブルを作成し、そのテーブルにDataRow(this.ObjectDataRow)をインポートしようとしました。 StringWriterから出力された文字列は空です( "") – Keith