2016-09-07 8 views
0

私は現在、データベースから多くのデータをインポートし、xsdスキーマに一致するxmlとして出力する必要がある場合に取り組んでいます。私はxsd.exeを使用して、特定のスキーマのC#クラスを生成しました。データベースからxsd.exeから作成されたcassを埋める方法

私の質問は、このクラスを使用してデータベースからデータを取り込む方法です。私はこの中に移入する「ttc_queue」の複数の要素を持つことになります `

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "TTC_Queue")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "MT_Queue", IsNullable = false)] 
    public partial class STAGING_Low 
    { 
     private object[] itemsField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("TTC_Queue", typeof(TTC_Queue))] 
     [System.Xml.Serialization.XmlElementAttribute("ROW_COUNTS", typeof(ROW_COUNTS))] 
     public object[] Items 
     { 
      get 
      { 
       return this.itemsField; 
      } 
      set 
      { 
       this.itemsField = value; 
      } 
     } 
    } 

    /// <remarks/> 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "TTC_Queue")] 
    public partial class TTC_Queue 
    { 
     private System.Data.SqlTypes.SqlDecimal reportingUnitCodeField; 

     private bool reportingUnitCodeFieldSpecified; 

     private System.Data.SqlTypes.SqlString preparerNField; 
     //more fields 

` 

私のクラスは次のようになります。 これで使用されるttc_queueのオブジェクト[]がすでに設定されています。

この配列を「アイテムフィールド」に設定してこれを逆シリアル化するにはどうすればよいですか?

は、私は現在持っている:

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Object cannot be stored in an array of this type.

私が欠けているかわからないんだけど:

STAGING_low low = new STAGING_Low(); 
low.Items = new TTC_Queue[1]; 
low.Items.SetValue(myObject[],0); 

私は値を設定しています私はエラーを取得します。

ご協力いただきありがとうございます。

答えて

0

この場合、シリアル化が正しいアプローチではないと思います。 SQLクエリを適切に生成すると、以下のコードがうまくいくはずです。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Data.SqlClient; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      DataSet ds = new DataSet(); 
      ds.ReadXmlSchema("filename"); 
      string SQL = "SELECT QUERY"; 
      string connStr = "Enter you connection string here"; 
      SqlDataAdapter adapter = new SqlDataAdapter(SQL, connStr); 
      adapter.Fill(ds); 

      ds.WriteXml("filename"); 
     } 
    } 
} 
+0

これは私が必要とするようにデータを作成して作成しました。ただし、各レコードについては、

というタグに配置されており、xsdの定義された要素タグではありません。どのように私はこれを変更できますか?また、別のテーブルから同じxmlに必要な他の要素をどのようにすることができますか?すべてのテーブルを1つのクエリに結合して、アダプタが認識してデータセットを読み込む方法を教えてください。この特定のユースケースでは、TTC_Queueの約20k要素とRoot_COUNTSの1要素がすべて同じルートにあります。より多くの情報が必要な場合はお知らせください。助けてくれてありがとう。 – orlando15767

+0

データセットは複数のテーブルで構成されているため、DataTable dt = new DataTable()を使用できます。アダプター。充填(dt); ds.Tables.Add(dt);各テーブルにはnameプロパティがあり、次のようにテーブル名を変更できます。dt.TableName = "New Table Name";またはコンストラクタnew DataTable( "テーブル名")。 DataTableでカラム名を変更するには、次のようにデータベースのクエリの 'AS'を使用します。SELECT COLA as abc – jdweng

+0

これは完全に機能しました。ありがとうございました。 – orlando15767

関連する問題