Converting XML-string into Dataset
-
I am having a hard time converting a string representation of a xml-document into a dataset. The case is: 1. I send a request to a server which answers with a xml-document in the form of a string. 2. Next I want to convert this string into a System.Data.DataSet and display it in a datagrid or some other. It is giving me a really hard time figuring out how to do this. I have tríed the following: { System.Xml.XmlDataDocument xmlDD = new System.Xml.XmlDataDocument(); System.Data.DataSet dsXML = new System.Data.DataSet(); xmlDD.LoadXml(strXml); dsXml = xmlDD.DataSet; } This will generate following error "The IListSource does not contain any data sources." I have tried the following, it works, but is a VERY bad idea for a webpage: { xmlDD.LoadXml(strXml); xmlDD.Save(Server.MapPath("log") + "\\out.xml"); dsXml.ReadXml("\\out.xml"); } Why can't I go directly from a string through a xmldocument to a dataset? Any comments appreciated. Preben Rasmussen IT-consultant
-
I am having a hard time converting a string representation of a xml-document into a dataset. The case is: 1. I send a request to a server which answers with a xml-document in the form of a string. 2. Next I want to convert this string into a System.Data.DataSet and display it in a datagrid or some other. It is giving me a really hard time figuring out how to do this. I have tríed the following: { System.Xml.XmlDataDocument xmlDD = new System.Xml.XmlDataDocument(); System.Data.DataSet dsXML = new System.Data.DataSet(); xmlDD.LoadXml(strXml); dsXml = xmlDD.DataSet; } This will generate following error "The IListSource does not contain any data sources." I have tried the following, it works, but is a VERY bad idea for a webpage: { xmlDD.LoadXml(strXml); xmlDD.Save(Server.MapPath("log") + "\\out.xml"); dsXml.ReadXml("\\out.xml"); } Why can't I go directly from a string through a xmldocument to a dataset? Any comments appreciated. Preben Rasmussen IT-consultant
One of the overloads of the
ReadXml
method accepts aTextReader
object. TheStringReader
class is derived fromTextReader
, so you can just do:System.IO.StringReader sr = new System.IO.StringReader(strXml);
System.Data.DataSet dsXML = new System.Data.DataSet();
dsXML.ReadXml(sr);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I am having a hard time converting a string representation of a xml-document into a dataset. The case is: 1. I send a request to a server which answers with a xml-document in the form of a string. 2. Next I want to convert this string into a System.Data.DataSet and display it in a datagrid or some other. It is giving me a really hard time figuring out how to do this. I have tríed the following: { System.Xml.XmlDataDocument xmlDD = new System.Xml.XmlDataDocument(); System.Data.DataSet dsXML = new System.Data.DataSet(); xmlDD.LoadXml(strXml); dsXml = xmlDD.DataSet; } This will generate following error "The IListSource does not contain any data sources." I have tried the following, it works, but is a VERY bad idea for a webpage: { xmlDD.LoadXml(strXml); xmlDD.Save(Server.MapPath("log") + "\\out.xml"); dsXml.ReadXml("\\out.xml"); } Why can't I go directly from a string through a xmldocument to a dataset? Any comments appreciated. Preben Rasmussen IT-consultant
I also find it annoying that there isn't a simple way of loading a dataset from a string. Here's one way that's a bit of a rigmarole but it does the job: Dim srForData As System.IO.StringReader = New System.IO.StringReader(StringToLoadFrom) Dim rdrForData As System.Xml.XmlReader = New System.Xml.XmlTextReader(srForData) DataSetToLoad = New DataSet() DataSetToLoad.ReadXml(rdrForData)
-
One of the overloads of the
ReadXml
method accepts aTextReader
object. TheStringReader
class is derived fromTextReader
, so you can just do:System.IO.StringReader sr = new System.IO.StringReader(strXml);
System.Data.DataSet dsXML = new System.Data.DataSet();
dsXML.ReadXml(sr);
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks a lot - it works great. Just what I wanted. Preben Rasmussen
-
I also find it annoying that there isn't a simple way of loading a dataset from a string. Here's one way that's a bit of a rigmarole but it does the job: Dim srForData As System.IO.StringReader = New System.IO.StringReader(StringToLoadFrom) Dim rdrForData As System.Xml.XmlReader = New System.Xml.XmlTextReader(srForData) DataSetToLoad = New DataSet() DataSetToLoad.ReadXml(rdrForData)
This one also works fine - thanks. Preben Rasmusen