Convert XML to String

B

Brian

I have an XML file that I want to read in and pass as a string to a
stored procedure. I am having trouble getting the file into a string.
I am using a TextReader to read the file in, but my string that I end
up with has a backslash character in front of all the double quotes.
How do I get a string from my file that doesn't have all these extra
characters? Is there an easier way to do this. Here is the code I am
using to read the file.

TextReader r = new StreamReader(c.FileName);
while ((input=r.ReadLine())!=null)
{
strXML += input;
}

Thanks,
Brian
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

Are you sure that the backslash is in the string, or is that what you
see in the debugger? If it is in the debugger, then that's just the
representation of special characters in the string, not what is actually in
the string itself.

Hope this helps.
 
D

Daniel O'Connell [C# MVP]

Brian said:
I have an XML file that I want to read in and pass as a string to a
stored procedure. I am having trouble getting the file into a string.
I am using a TextReader to read the file in, but my string that I end
up with has a backslash character in front of all the double quotes.

I assume you are seeing this in the debugger.
The debugger uses C# syntax to express strings clearly to the user. It does
not mean that the slashes are there in the actual string.
 
S

Stefan Simek

Also, the TextReader has a ReadToEnd() method that can read the whole file
at once. String concatentation results in lots of unneeded allocations and
copying.

HTH,
Stefan

Nicholas Paldino said:
Brian,

Are you sure that the backslash is in the string, or is that what you
see in the debugger? If it is in the debugger, then that's just the
representation of special characters in the string, not what is actually
in the string itself.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Brian said:
I have an XML file that I want to read in and pass as a string to a
stored procedure. I am having trouble getting the file into a string.
I am using a TextReader to read the file in, but my string that I end
up with has a backslash character in front of all the double quotes.
How do I get a string from my file that doesn't have all these extra
characters? Is there an easier way to do this. Here is the code I am
using to read the file.

TextReader r = new StreamReader(c.FileName);
while ((input=r.ReadLine())!=null)
{
strXML += input;
}

Thanks,
Brian
 

Ask a Question

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.

Ask a Question

Top