Solved

convert System.IO.StringWriter to int c#

Posted on 2009-07-07
7
1,560 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
REXEX help Part 2 2 26
Batch file or script with password 22 45
Footer for each row on Gridview 2 23
RegEx with optional part 4 28
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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