Solved

convert System.IO.StringWriter to int c#

Posted on 2009-07-07
7
1,533 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now