C # File.Open and Stream equivalent

This web service expects this XML file:

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

      

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

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

      

the stream is a standard .net MemoryStream

what operation am I doing on the thread to make it the same as File.Open?

Thanks!!

check this (api definition):

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

      

+1


a source to share


5 answers


I doubt the webservice is actually expecting a thread. How would it be? Are you sure this isn't just waiting for the content as an array of bytes?

File.Open

returns a FileStream

- if stream

there is already there FileStream

is no difference between them. It is just possible that he wants FileStream

it when you just got it stream

. If this is the case and it really isn't from a file, then you probably have to write it to a file and open it for it FileStream

. Then, complain to webservice developers that their API is fancy.



EDIT: if it only expects stream

you should be fine. You say it hangs - have you tried debugging and seeing exactly where this hangs? Is it trying to read more data for some reason?

+4


a source


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

The most common mistake I make when going from file stream to memory stream is to miss that after writing to memory stream I am at the end:



stream.Seek(0, SeekOrigin.Begin);

      

+1


a source


Have you tried putting the contents of the file on a memory stream before assigning it? The service may be using some stream functionality that your file stream does not support.

Something like that:

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

      

EDIT:

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

Now, I suggest you compare exactly the contents of the memory stream with the file stream. Perhaps a different encoding?

+1


a source


File.Open returns a stream. One more problem.

0


a source


File.Open returns a FileStream opened at the beginning of the stream.

You posted the code:

request.FeedContent = stream;

      

does not indicate the state of the stream you are assigning to FeedContent. For instance. perhaps you are not positioned at the beginning of the file? (use Stream.Seek if the stream supports searching, what happens if it's FileStream or MemoryStream).

Please post more code if it doesn't help.

0


a source







All Articles