Convert XML to String

  • Thread starter Thread starter Brian
  • Start date Start date
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
 
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.
 
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.
 
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
 
Back
Top