How to convert a string to stream

Source: Internet
Author: User
How do you convert a String object to stream (type) object?

I know it is more common to do the conversion in the other direction, but for me it is definitely the other way.

I have a web service input parameter of Type string. this parameter is actually an XML document payload. why this is so is a long story.

Anyway, I need to validate it. I 've written XML validation code in the past. The one real stumbling block here ISD this data type conversion.

I'm at a bit of a loss how to do this. ultimately I need to get the data into an xmlreader object coupled with the xmlreadersettings object (this is. NET 2.0 Asp.net APP ). looking at the object model for various system. XML namespace objects this does not appear to be a straightforward task.

Short of writing the string to disk and then opening it up with a filestream object, there's got to be a better way. Anyone have a better idea?

Thanks!

Mr bungle
Tuesday, July 18,200 6

Deleting... Approving...

Implement a class derived from stream, I. e. Declare a stringstream class, use your string to construct it, and implement the various abstract methods declared in stream such as read and canwrite. Christopher Wells
Tuesday, July 18,200 6

Deleting... Approving...

Use the stringreader class, e.g.

String xmltext =... Your XML ...;
Xmlreadersettings settings =... your settings ...;

Stringreader = new stringreader (xmltext );
Xmltextreader xmlreader = xmlreader. Create (stringreader, settings );
... Joe
Wednesday, July 19,200 6

Deleting... Approving...

There is also the memorystream object, convert your string into an array of bytes and write directly into the memorystream.

Memorystream memstream = new memorystream ();
Byte [] DATA = encoding. Unicode. getbytes (thestring );
Memstream. Write (data, 0, Data. Length );
Foxedup
Wednesday, July 19,200 6

Deleting... Approving...

The system. Io. stringreader object worked like a charm and just required a single line of additional code. What a relief! Thanks for all the helpful replies. Mr bungle
Wednesday, July 19,200 6

Deleting... Approving...

This topic is archived. No further replies will be accepted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.