Scott Kramer
Scott Kramer

Reputation: 1731

C# File.Open and Stream equivalent

This webservice expects this xml file:

request.FeedContent = File.Open("test.xml", FileMode.Open, FileAccess.Read);

I already have the file in a stream, but this statement hangs:

stream.Position = 0;
request.FeedContent = stream;

the stream is a standard .net MemoryStream

what operation do I do on the stream to make it the same as File.Open?

Thanks!!

check this out (api definition):

    /// <summary>
    /// Gets and sets the FeedContent property.
    /// </summary>
    //[XmlElementAttribute(ElementName = "FeedContent")]
    public Stream FeedContent
    {
        get { return this.feedContentField ; }
        set { this.feedContentField= value; }
    }

Upvotes: 3

Views: 13480

Answers (5)

VVS
VVS

Reputation: 19612

Have you tried putting the file content into a memory stream before assigning it? Maybe the service is using some stream features that your file stream doesn't support.

Something like this:

var stream = new MemoryStream(File.ReadAllBytes(fileName));
request.FeedContent = stream;

EDIT:

Wait... I totally misunderstood your question. So the version where you directly pass a FileStream works and if you provide a MemoryStream with the same content it doesn't?

Now I would suggest you to exactly compare the content of the memory stream with that of the file stream. Perhaps a different encoding?

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1504122

I doubt that the web service really expects a stream. How on earth would it represent that? Are you sure it doesn't just expect the contents as a byte array?

File.Open just returns a FileStream - if stream is already a FileStream then there's no difference between the two. It's just possible that it wants a FileStream whereas you've just got a Stream. If that's the case and it's really not from a file, you'll potentially have to write it to a file and open a FileStream to that. Then complain to the webservice developers that their API is bizarre.

EDIT: If it's only expecting a Stream, you should be fine. You say that it hangs - have you tried debugging and seeing where exactly it's hanging? Is it trying to read more data for some reason?

Upvotes: 4

Julien Roncaglia
Julien Roncaglia

Reputation: 17837

Are you sure that the current position in your stream is the correct one?

The most common error i do when moving from a file stream to a memory stream is to miss that after writing to the memory stream i am at the end of it :

stream.Seek(0, SeekOrigin.Begin);

Upvotes: 1

to StackOverflow
to StackOverflow

Reputation: 124814

File.Open returns a FileStream that is open and positioned at the beginning of the stream.

The code you posted:

request.FeedContent = stream;

doesn't indicate the state of the stream you are assigning to FeedContent. E.g. maybe you aren't positioned at the start of the file? (use Stream.Seek if the stream supports seeking, which it will if it's a FileStream or MemoryStream).

Post more code if this doesn't help.

Upvotes: 0

spender
spender

Reputation: 120548

File.Open returns a stream. Something else is the problem.

Upvotes: 0

Related Questions