Solved

convert System.IO.StringWriter to int c#

Posted on 2009-07-07
7
1,601 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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