Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert System.IO.StringWriter to int c#

Posted on 2009-07-07
7
Medium Priority
?
1,677 Views
Last Modified: 2013-12-17
i need to convert System.IO.StringWriter to an int c#.

it only contains a single value extracted from an xml, but i can not convert it to an int or a string

i get the following errors

when trying to convert to a string
Cannot implicitly convert type 'string' to 'System.IO.StringWriter'

when trying to convert to an int.

{"Unable to cast object of type 'System.IO.StringWriter' to type 'System.IConvertible'."}

Thank You
StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        XslCompiledTransform pagingXSLT = new XslCompiledTransform();
        pagingXSLT.Load("paging.xslt");
        pagingXSLT.Transform(xmlDoc, null, sw);
 
        int total_rows = 0;
        if (sw == null)
        {
           total_rows = 0;
        }
        else
        {
            total_rows = Convert.ToInt32(sw);
        }

Open in new window

0
Comment
Question by:QuinnDester
  • 3
  • 3
7 Comments
 
LVL 20

Expert Comment

by:ddayx10
ID: 24799209
Well I'm not familiar with what you are doing there with the XslCompiledTransform class, but I don't want you to be ignored so I'll throw my take at it.

StreamWriter is not going to be convertible to Int. There is nothing in that class that would be convertible. The class is used to write text to the stream, say writing to a file.

If I were to guess(and let's be real that's all I'm doing) the XslCompiledTransform.Transform() method is using the StreamWriter class to write to your XML file (xmlDoc). The value you want to convert is likely in there and you would use StreamReader to pull it out and convert it to an int.

I could be off base about how the XslCompiledTransform class works, but still there's nothing in your variable sw to convert to an int.
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 24799517
Thanks for your reply

its working the other way around the the xmlDoc is being transformed by the XslCompiledTransform
and the value that is pulled out of the xmlDoc is passed to sw, i have checked it using debugger and sw does hold the value i need, i just cant get it into a type i can use.

i am assuming i will need to pass it to a different object other than StreamWriter but dont know what to  use.
 i have tried to pass it directly to a string and an int but neither of those will compile so what i am looking for is a middle man that XslCompiledTransform can pass the value to and can be converted to either an int or a string
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24799710
Interresting I knew that was your contention, but it didn't seem right. The only place in a StreamWriter that contains the info it's been writing that I'm aware of is the charBuffer??

http://msdn.microsoft.com/en-us/library/ms163439.aspx tends to indicate you are right.

Where do you see your value in the StreamWriter? In the above documentation it doesn't show how to utilize the StreamWriter after the Transform has taken place...nice of them.

Anyway, it took me awhile to think this through and I think I see now :)

The StringWriter is outputting into your StringBuiilder (sb) and therefore you need to get your string out of the sb and then convert it to an int...

Convert.ToInt32(sb.ToString());
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 20

Expert Comment

by:ddayx10
ID: 24799712
That's my guess and I'm not going to think about it anymore...
0
 
LVL 3

Author Comment

by:QuinnDester
ID: 24801273
If you stick in a Breakpoint and hover over the sw the value is dispalyed.
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 2000 total points
ID: 24801579
Flush the stringwriter before conversion

StringBuilder sb = new StringBuilder();

StringWriter sw = new StringWriter(sb);
StringWriter.Flush();

Convert.ToInt32(sb.ToString());
0
 
LVL 3

Author Closing Comment

by:QuinnDester
ID: 31600823
Excellent, That works brilliantly

Thank You
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question