2010-04-19 22 views
2

我已經使用以下代碼將數據集保存在xml列中的sql數據庫中。Dataset.ReadXml() - 給定編碼中的字符無效

XmlDataDocument dd = new XmlDataDocument(dataset); 

,並把該XML文檔作爲SQL參數使用

param.value = new XmlNodeReader(dd); 

的XML就像

<NewDataSet><SubContractChangeOrders><AGCol>1</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>006</Contract_x0020_Number><ContractID>30</ContractID><ChangeOrderID>211</ChangeOrderID><Amount>0.0000</Amount><Udf_CostReimbursableFlag>false</Udf_CostReimbursableFlag><Udf_CustomerCode /><Udf_SubChangeOrderStatus /></SubContractChangeOrders><SubContractChangeOrders><AGCol>2</AGCol><SCO_x0020_Number>002</SCO_x0020_Number><Contract_x0020_Number>006</Contract_x0020_Number><ContractID>30</ContractID><ChangeOrderID>212</ChangeOrderID><Amount>0.0000</Amount><Udf_CostReimbursableFlag>false</Udf_CostReimbursableFlag></SubContractChangeOrders><SubContractChangeOrders><AGCol>3</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>111</Contract_x0020_Number><ContractID>87</ContractID><ChangeOrderID>12</ChangeOrderID><Amount>300.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>4</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>222</Contract_x0020_Number><ContractID>80</ContractID><ChangeOrderID>6</ChangeOrderID><Amount>100.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>5</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>777</Contract_x0020_Number><ContractID>79</ContractID><ChangeOrderID>5</ChangeOrderID><Amount>200.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>6</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>786</Contract_x0020_Number><ContractID>77</ContractID><ChangeOrderID>3</ChangeOrderID><Amount>100.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>7</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>787</Contract_x0020_Number><ContractID>78</ContractID><ChangeOrderID>4</ChangeOrderID><Amount>500.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>8</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con 009</Contract_x0020_Number><ContractID>219</ContractID><ChangeOrderID>78</ChangeOrderID><Amount>9000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>9</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con 010</Contract_x0020_Number><ContractID>220</ContractID><ChangeOrderID>79</ChangeOrderID><Amount>13000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>10</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con 012</Contract_x0020_Number><ContractID>222</ContractID><ChangeOrderID>83</ChangeOrderID><Amount>2300.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>11</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con 020</Contract_x0020_Number><ContractID>226</ContractID><ChangeOrderID>86</ChangeOrderID><Amount>5400.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>12</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con 021</Contract_x0020_Number><ContractID>227</ContractID><ChangeOrderID>87</ChangeOrderID><Amount>2300.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>13</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con001</Contract_x0020_Number><ContractID>208</ContractID><ChangeOrderID>72</ChangeOrderID><Amount>3000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>14</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con002</Contract_x0020_Number><ContractID>209</ContractID><ChangeOrderID>73</ChangeOrderID><Amount>400.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>15</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con003</Contract_x0020_Number><ContractID>210</ContractID><ChangeOrderID>74</ChangeOrderID><Amount>6000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>16</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con004</Contract_x0020_Number><ContractID>211</ContractID><ChangeOrderID>75</ChangeOrderID><Amount>9000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>17</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Con005</Contract_x0020_Number><ContractID>213</ContractID><ChangeOrderID>76</ChangeOrderID><Amount>17000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>18</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>Cont001</Contract_x0020_Number><ContractID>228</ContractID><ChangeOrderID>89</ChangeOrderID><Amount>2000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>19</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>PUR001</Contract_x0020_Number><ContractID>229</ContractID><ChangeOrderID>88</ChangeOrderID><Amount>1000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>20</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>PUR002</Contract_x0020_Number><ContractID>230</ContractID><ChangeOrderID>90</ChangeOrderID><Amount>3000.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>21</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>SC-002</Contract_x0020_Number><ContractID>2</ContractID><ChangeOrderID>7</ChangeOrderID><Amount>200.0000</Amount></SubContractChangeOrders><SubContractChangeOrders><AGCol>22</AGCol><SCO_x0020_Number>001</SCO_x0020_Number><Contract_x0020_Number>SC-004</Contract_x0020_Number><ContractID>7</ContractID><ChangeOrderID>65</ChangeOrderID><Amount>1000.0000</Amount></SubContractChangeOrders></NewDataSet> 

我想如下

using (SqlConnection con = new SqlConnection("Server=#####;Initial Catalog=#####;User ID=####;Pwd=########")) 
     { 
      using (SqlCommand com = new SqlCommand("select * from dbo.tbl_#####", con)) 
      { 
       using (SqlDataAdapter ada = new SqlDataAdapter(com)) 
       { 
        ada.Fill(dt); 
        MemoryStream ms = new MemoryStream(); 
        object contractXML1 = dt.Rows[0]["SCOXML1"]; 
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
        bf.Serialize(ms, contractXML1); 
        ms.Seek(0, SeekOrigin.Begin); 
        ds.ReadXml(ms); 
       } 
      } 
     } 
讀回

我收到以下錯誤

根級別的數據無效。行1,位置6.

任何想法?


更新

我改變了代碼以下內容。我能夠獲得其中添加了所有表的數據集的結構。但不是數據。

using (SqlConnection con = new SqlConnection("Server=###;Initial Catalog=####;User ID=###;Pwd=######")) 
     { 
      using (SqlCommand com = new SqlCommand("select * from dbo.tbl_####", con)) 
      { 
       using (SqlDataAdapter ada = new SqlDataAdapter(com)) 
       { 
        ada.Fill(dt); 
        MemoryStream ms = new MemoryStream(); 
        object contractXML1 = dt.Rows[0]["SCOXML1"]; 
        object schema = dt.Rows[0]["xmlSchemaHistory"]; 
        XmlSerializer xs = new XmlSerializer(typeof(object)); 
        xs.Serialize(ms, contractXML1); 
        ms.Position = 0; 
        xDoc.Load(ms); 
        xDocSc.LoadXml(schema.ToString()); 
        ds.ReadXmlSchema(new XmlNodeReader(xDocSc)); 
        ds.ReadXml(new XmlNodeReader(xDoc)); 
       } 
      } 
     } 

但是底層的xml文檔是有數據的。有任何想法嗎?

NLV

回答

5

爲什麼使用二進制格式化程序?事實上,你爲什麼要序列化呢?您的XML列是否使用SQL Server XML數據類型?如果是這樣,那麼您需要使用SqlXml數據類型檢索數據。


我從來沒有與XML列數據表工作。爲此我通常使用SqlDataReader。但是,我可以給你一個提示:一旦你有一個SqlXml對象,你可以從中獲得XmlReader。一旦你得到了一個XmlReader,你可以這樣做:

DataTable dt = new DataTable("tableName"); 
dt.ReadXml(xmlReader); 

請參閱 「ReadXml(XmlReader)」 和 「SqlXml.CreateReader Method」。

+0

好,我甚至沒有檢查他在串行器上做了什麼。 +1代碼審查。 – 2010-04-19 18:24:08

+0

如何將SqlXml轉換爲數據表?任何見解?另外我正在努力將對象轉換爲SqlXml。 – NLV 2010-04-19 18:40:14

0

這是一個在黑暗中拍攝,但你的XML可能在你的XML字節順序標記(BOM)和二進制格式化無法正常沒有指定的編碼處理它。如果是這種情況,那麼您可能必須首先將XML加載到編碼流中。

相關問題