2016-09-07 18 views
0

현재 데이터베이스에서 많은 데이터를 가져 와서 xsd 스키마와 일치하는 xml로 출력해야하는 경우를 처리하고 있습니다. xsd.exe를 사용하여 특정 스키마에 대한 C# 클래스를 생성했습니다.C# 데이터베이스에서 xsd.exe에서 만든 채우기 방법

내 질문은 어떻게이 클래스를 사용하여 데이터베이스에서 데이터를 채울 수 있습니까?

내 클래스는 다음과 같다 :`

[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"여러 요소가됩니다. 여기에 이미 ttc_queue의 객체 []가 채워져 있습니다.

"항목 필드"로이 배열을 설정 한 다음이 또한 deserialize합니까?

는 내가 현재 가지고 :

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

필자는 필자가 필요로하는 것처럼 데이터를 작성하고 XML로 작성했습니다. 그러나 각 레코드는

태그에 있고 xsd에 정의 된 요소 태그가 아닙니다. 어떻게 수정할 수 있습니까? 또한 다른 테이블에서 동일한 XML로 필요한 다른 요소를 어떻게 처리 할 수 ​​있습니까? 어떻게하면 모든 테이블을 하나의 쿼리로 결합하여 어댑터가 인식하고 데이터 세트를 읽을 수 있습니까? 이 특정 유스 케이스의 경우 TTC_Queue의 대략 20,000 개 요소와 모두 동일한 루트에있는 ROW_COUNTS 중 하나를 갖게됩니다. 더 많은 정보가 필요하면 알려주십시오. 도움을 주셔서 감사합니다. – orlando15767

+0

데이터 집합은 여러 테이블로 구성되어 있으므로 DataTable dt = new DataTable(); adapter.Fill (dt); ds.Tables.Add (dt); 각 테이블에는 name 속성이 있으므로 다음과 같이 테이블 이름을 변경할 수 있습니다. dt.TableName = "New Table Name"; 또는 생성자 new DataTable ("table name"); 다음과 같이 데이터베이스에 대한 쿼리에서 'AS'를 사용하여 DataTable의 열 이름을 변경할 수 있습니다. SELECT COLA as abc – jdweng

+0

이것은 완벽하게 작동했습니다. 고맙습니다. – orlando15767