2009-01-22 60 views
0

1)I做Dataset.WriteToXml(存储器流对象) 2)然后创建一个XMLDocument对象 3)I然后XMLDocument.load方法(存储器流对象)根命名空间中DataSet.WriteToXml

我失踪得到“未找到XML根名称空间”异常。

数据集XML是否不包含所需的名称空间?

谢谢。

回答

1

您是否在尝试将其加载到XmlDocument之前重新定位您的内存流?

DataSet ds = new DataSet(); 
using (SqlConnection connection = new SqlConnection("some connection string")) 
using (SqlCommand command = new SqlCommand("select * from mytable", connection)) 
using (SqlDataAdapter adapter = new SqlDataAdapter(command)) 
{ 
    adapter.Fill(ds); 
} 

XmlDocument xml = new XmlDocument(); 
using (Stream stream = new MemoryStream()) 
{ 
    ds.WriteXml(stream); 
    // We must reposition the memory stream before loading the xml 
    stream.Position = 0; 
    xml.Load(stream); 
}