2010-09-08 50 views
1

通常我編寫一個類併爲其添加XML序列化以用於我的Web服務。C#如何填充使用數據庫中的XSD.EXE創建的類

[XmlRootAttribute(ElementName = "dsXmlSummary", IsNullable=true)] 
public class Class1 
{ 
    //declare properties 
    //make database calls to pull data and load properties 
} 

我上要求我使用了嚴格的XSD一個項目,我已經按照有關使用XSD.EXE工具來創建基於XSD類的指令。我的解釋是,這個自動生成的類將取代我的正常序列化類。

如果是這種情況,我完全無法將數據加載到類屬性中。 我已經通過收集從另一個散步:

[WebMethod] 
public dsXmlSummary getXML() 
{ 
    TextReader reader = new StreamReader("data.xml"); 
    dsXmlSummary ds = (dsXmlSummary)serializer.Deserialize(reader); 
    reader.Close(); 
} 

但是我已經位於一個SQL數據庫中的數據...我想我應該可以寫一個方法來填補dsXmlSummary類,但是我找不到任何文件。所有示例都如上所述,從實際的物理xml文檔加載或讀取。

我嘗試測試了手動負載:

[WebMethod] 
    public dsXmlSummary getXML() 
    { 
     dsXmlSummary xml = new dsXmlSummary(); 
     xml.Items[0].nameFirst = "Mark"; //error thrown here: System.NullReferenceException: Object reference not set to an instance of an object. 
     xml.Items[0].nameLast = "Twain"; 
     xml.Items[0].Type = "Writer"; 

     return xml; 
    } 

很顯然,我會對此完全錯誤的。任何指導非常感謝。

編輯

我的WebMethod

 [WebMethod] 
    public dsXmlSummary getXML() 
    { 
     dsXmlSummary xml = new dsXmlSummary(); 
     dsXmlSummaryAdmin_reports_xmlReports[] items = new dsXmlSummaryAdmin_reports_xmlReports[1]; 
     items[0].nameFirst = "Mark"; //error still thrown here: System.NullReferenceException: Object reference not set to an instance of an object. 
     items[0].nameLast = "Twain"; 
     items[0].Type = "Writer"; 

     xml.Items = items; 
     return xml; 
    } 

自動生成的類

public partial class dsXmlSummary { 

private dsXmlSummaryAdmin_reports_xmlReports[] itemsField; 

/// <remarks/> 
[System.Xml.Serialization.XmlElementAttribute("admin_reports_xmlReports")] 
public dsXmlSummaryAdmin_reports_xmlReports[] Items { 
    get { 
     return this.itemsField; 
    } 
    set { 
     this.itemsField = value; 
    } 
    } 
} 

回答

3

如果您以字符串的形式從數據庫中獲取XML,則可以使用StringReader。或者如果您收到byte[],您可以嘗試MemoryStream

[WebMethod] 
public dsXmlSummary getXML() 
{ 
    TextReader reader = new StringReader("<dsXmlSummary><FirstName>Mark</FirstName></dsXmlSummary>"); 
    dsXmlSummary ds = (dsXmlSummary)serializer.Deserialize(reader); 
    reader.Close(); 
} 

關於你的「手動」例子,你需要初始化你的Items數組。
<編輯>新增xml.Items[0] = new YourItemsType(); < /編輯>

[WebMethod] 
public dsXmlSummary getXML() 
{ 
    dsXmlSummary xml = new dsXmlSummary(); 
    xml.Items = new YourItemsType[1]; // <-- initialize here 
    xml.Items[0] = new YourItemsType(); // <-- initialize first object 
    xml.Items[0].nameFirst = "Mark"; //error thrown here: System.NullReferenceException: Object reference not set to an instance of an object. 
    xml.Items[0].nameLast = "Twain"; 
    xml.Items[0].Type = "Writer"; 

    return xml; 
} 
+0

我想這是我的問題。我知道如何從流到序列化...但是,數據庫中的數據不是以XML的形式構造的。我寫了一個從數據庫中提取數據的類,然後將類本身設置爲可串行化,以設置xml屬性...我只是不確定如何從類到流。 – jon3laze 2010-09-08 21:57:15

+0

@ jon3laze:糟糕,忘了初始化第一個元素,糾正我的例子,再試一次。 – 2010-09-09 05:00:17

0

至於我記得,你可以得到的數據集/ DataTable的XML(的ToString()?或者可能是WriteXML)_,不確定)..然後用那個

+0

對不起,我的問題解釋不清楚。我編輯它,希望它現在更有意義。儘管感謝您的回覆。 – jon3laze 2010-09-08 04:04:29

+0

阿哈..對不起,我有點撇取你的問題..無論如何,我猜阿爾賓的答案現在是足夠的 – 2010-09-08 05:08:23

相關問題