Big Posted November 5, 2003 Posted November 5, 2003 Hi, I got the next problem: I have a string, and that string contains XML Example: "<XML></XML>" Problem: I want to read the XML in the string into a dataset but i don 't want to save the string to a file first. The string i get from a dataset. And to write XML you can use dataset.getXML and put that in a string. Then write the string to a db. Then i get the string from the db and i would like to get it back in a dataset. Thx L Quote
XyBoy Posted November 5, 2003 Posted November 5, 2003 So you want to serialize a whole DataSet into an XML string, and store this string into a DataBase (so that 1 single field of your database would be used to recreate a full DataSet) ? There is indeed a problem to Write/Read XML to/from a string. The methods ReadXml and WriteXml work with streams, files or XMLReader. The GetXml returns a string, but it can only be used to write the string, and you cannot be sure that it is fully compatible with the ReadXml method. So I would use ReadXml and WriteXml. The problem is : how to access a string ? Well, there are several solutions : 1. you can try to make a stream from your string. Have a look at the System.IO.MemoryStream class. You create a stream of a given length, like this : MemoryStream memStream = new MemoryStream(yourString.Length) then you can write to the memory stream (the memory stream is just a kind of array of bytes that implements some standard methods for writing/reading/seeking data) : memStream.Write(yourString, 0 , yourString.Length); Now you have a stream that contains your XML, and you can read it with the ReadXml method. To write the XML back to the DB, it is the same (unless you use the GetXml method) : you create a MemoryStream of a sufficient length, and then you call WriteXml to write the DataSet's content into the stream. Reading the content of the stream into a string is very easy ("memStream.Read (yourString, 0, x);"). 2. you can create a TextReader from your string. Use the System.IO.StringReader class, like this : StringReader strReader = new StringReader(yourString); Then pass this reader to the ReadXml method. To write the XML back, you can probably use the WriteXml method with a TextWriter (use the System.IO.StringWriter class, and its ToString() method to get the corresponding string). 3. create a XmlReader from your string. Use the System.Xml.XmlTextReader class. Create it with a TextReader created as in case 2 : XmlTextReader yourXmlReader = new XmlTextReader (new StringReader (yourString)); The only advantage of this solution is that it checks that the string is well-formed XML. I don't think it is really necessary, since the DataSet's ReadXml method will probably check it also when you give it a TextReader (case 2 ; actually I think that it checks the content of the TextReader as described here, by creating a XmlTextReader from it). Conclusion : use case 2 ! Hope this helps ! Olivier. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.