Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How do I get this into a string variable?
Message
From
13/05/2008 09:24:57
Mike Sue-Ping
Cambridge, Ontario, Canada
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01316145
Message ID:
01316629
Views:
17
>>>>>>Newbie question. I have the following snippet of C# code that writes the contents of "SearchResponse" to a file. How can I get the contents into a string variable instead?
>>>>>>
>>>>>
>>>>>Try something like this:
>>>>>
>>>>>
>>>>>XmlSerializer xmlizer = new XmlSerializer(typeof(SearchResponse));
>>>>>StringWriter sw = new StringWriter();
>>>>>xmlizer.Serialize(sw, iw.SearchResponse);
>>>>>
>>>>
>>>>Hi Paul,
>>>>
>>>>Can you suggest how I can get the following into a string instead of a file?
>>>>
>>>>
>>>>FileStream fs = new FileStream("c:/xmlresponse2.xml", FileMode.Create);
>>>>fs.Write(m_searchresponse.Manifest[0].Content.Value, 0, m_searchresponse.Manifest[0].Content.Value.Length);
>>>>fs.Close();
>>>>
>>>>
>>>
>>>Maybe (assuming m_searchresponse.Manifest[0].Content.Value is a byte[]) :
>>>
>>>
>>>string result = System.Text.Encoding.ASCII.GetString(m_searchresponse.Manifest[0].Content.Value);
>>>
>>
>>Thanks Paul. You are correct in assuming that the object is a byte[]. Your command worked except for one small issue. At the end of the string is some text that messes up the html. It is:
>
>>
>>If I remove it manually things are good. Any idea why this "extra junk" is getting there?
>>
>
>Sorry, I'm not sure what I'm supposed to be seeing from the HTML you posted. Is this the string you're generating from m_searchresponse.Manifest[0].Content.Value? Just a guess but maybe the encodings wrong - try System.Text.Encoding.UTF8.GetString() instead?

Paul, I probably didn't explain it very well. What is happening when I view the contents of the string in IE7 is at the bottom of the page, there is an error:
The XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 


--------------------------------------------------------------------------------

Cannot have a DOCTYPE declaration outside of a prolog. Error processing resource 'http://localhost:1258/WebSite4/Default.as...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&...
rgin-left:1em;text-indent:-2em">   <ClosedDate>2007-06-26-06:00</ClosedDate> 
- <Properties>
- <Property>
<DIV
When I view the source, I see what appears to be normal XML except at the very bottom is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
	Untitled Page
</title></head>
<body>
    <form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGTDH6DkceZcgjl3lgkIoHipA0YqKg==" />
</div>

    <div>
    
    </div>
    </form>
</body>
</html>
Immediately before the text shown above is the normal looking XML. If I manually remove the text above, everything is fine. Can you think of a way to programatically eliminate the last chunk of text?

Mike
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform