Solved

convert System.IO.StringWriter to int c#

Posted on 2009-07-07
7
1,628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

635 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