469,275 Members | 1,850 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

How to open XmlReader from string?

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!

Jun 11 '07 #1
7 43929
Katit <id*********@gmail.comwrote:
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?
Use XmlReader.Create passing in a StringReader created from the string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 11 '07 #2
I found this on google:

XmlTextReader reader = new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(myXMLs tring)));

I am attemting this as we speak.

~GINA_M~

Jun 11 '07 #3
Gina_Marano <gi*******@gmail.comwrote:
I found this on google:

XmlTextReader reader = new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(myXMLs tring)));

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 12 '07 #4
On Jun 12, 11:24 am, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Gina_Marano <ginals...@gmail.comwrote:
I found this on google:
XmlTextReader reader = new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(myXMLs tring)));
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.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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(myXMLs tring))); // Create
a memory stream object and dispose it.

I would suggest using the 'using' scope. E.g.

using (FileStream fStream = new FileStream(filePath,
FileMode.Create, FileAccess.Write))
{
using (StreamWriter writer = new
StreamWriter(fStream))
{
writer.WriteLine(myList.ToString());
writer.Close();
}
fStream.Close();
}
Jun 12 '07 #5
Aneesh Pulukkul[MCSD.Net] <an******@gmail.comwrote:
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(myXMLs tring))); // Create
a memory stream object and dispose it.

I would suggest using the 'using' scope. E.g.
While I would normally agree with you, there are no files involved here
in the first place - with both MemoryStream and StringReader, the only
resources involved are in memory.

When using XmlReader, I believe that closing the XmlReader will close
any streams it's been passed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 12 '07 #6
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Aneesh Pulukkul[MCSD.Net] <an******@gmail.comwrote:
>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(myXML string))); // Create
a memory stream object and dispose it.

I would suggest using the 'using' scope. E.g.

While I would normally agree with you, there are no files involved here
in the first place - with both MemoryStream and StringReader, the only
resources involved are in memory.

When using XmlReader, I believe that closing the XmlReader will close
any streams it's been passed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 12 '07 #7
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
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.
Ah - thanks for clearing it up.
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).
I think in this case I'd see how easy it was to do. It may be
relatively tricky to wrap a using statement round the lifetime of the
XmlReader, given that you certainly shouldn't close the stream just
after creating the reader.

Agreed on the general point of not coding to implementation though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Erik Wikström | last post: by
reply views Thread by William Stacey [MVP] | last post: by
37 posts views Thread by Vadim | last post: by
1 post views Thread by Deecrypt | last post: by
4 posts views Thread by Steph | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.