473,382 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 44495
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
0
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
37
by: Vadim | last post by:
Hi! I have a STL string, which I need to convert to low case. In VC 6 I used: string sInputBuffer = _strlwr((char*)sBuffer.c_str()); However, now in MSVC.NET 2005 I am getting a warning, that...
1
by: Deecrypt | last post by:
Hi, Passing a string parameter to a webservice to get the dataset generated as the result, seems to give me an error suggesting that no parameter is being passed. However it works if I explicitly...
4
by: Steph | last post by:
hello, because msdn is so poor... somebody did he write a similar script ? (DTE/DTE2) _applicationObject.Documents.Open(docPath, "?string kind?", false); i must type the "string kind"... but...
1
by: Andrus | last post by:
I need to remove all comments ( between <!-- and --tags) from XML string. I tried the following code but comments are still present. Or is it better to open xml string with a StreamReader, read...
0
sivisr
by: sivisr | last post by:
Hi Everybody, I need some help on web service.I want to open pdf file from database to my windows application using web service.Every thing was completed running correctly, but i could...
1
by: star01daisy | last post by:
This is what i have, but when i run the program the only thing that comes out is the inro line "This program opens...". What do i have to do for it to run smoothly.The program is supposed to sort the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.