K
Katit
I have string with XML, need to read it using XmlReader. I don't see
any way to feed string variable in there.
How it's done?
Thanks!
any way to feed string variable in there.
How it's done?
Thanks!
Katit said:I have string with XML, need to read it using XmlReader. I don't see
any way to feed string variable in there.
How it's done?
Gina_Marano said:I found this on google:
XmlTextReader reader = new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(myXMLstring)));
I am attemting this as we speak.
That's awful code:
1) Using ASCII (which is what it *looks* like it would do) would be a
bad idea to start with - any non-ASCII characters would be lost
2) Using the platform default encoding (which is what it *actually*
uses) is a bad idea for the same reason
3) It's doing a conversion for no reason
StringReader is safer, simpler and more efficient.
Aneesh Pulukkul said:One more thing, while dealing with streams, be sure that we close/
dispose the stream objects properly else we'll get he access denied/
file is in use by another process issues.
new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(myXMLstring))); // Create
a memory stream object and dispose it.
I would suggest using the 'using' scope. E.g.
Nicholas Paldino said:If you call the overload of Create on XmlReader that takes just a
TextReader implementation, then no, the XmlReader will not close the
underlying reader. You have to pass an XmlReaderSettings to the Create
method setting the CloseInput property to true.
It's probably not the worst idea in the world though to use it in a
using statement, even though you are using a StringReader. By skipping it
(kind of like not calling Dispose on the DataSet) you are coding based on an
implementation detail, which as we all know, is bad (as a general statement,
of course, there are instances where you are forced to code against
implementation details).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.