trying to instantiate XPathDocument with MemoryStream

D

D

Hi,

I am trying to instantiate an instance of XPathDocument after serializing
XML into a memory stream. Here is my method that uses XmlSerializer


public MemoryStream SerializeXMLSelectedApplication()
{
System.IO.MemoryStream stream = new MemoryStream();
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Subject));
serializer.Serialize(stream, this);
}
catch(SystemException ex)
{
Console.WriteLine(ex.Message);
}
return stream;
}


//Using the return stream, I attempt to instantiate XPathDocument

XPathDocument doc = new
XPathDocument(session.Subject.SerializeXMLSelectedApplication());

The line of code above causes an error message that reads "root element
missing." So I am thinking that I am not using my memory stream properly
for the XPathDocument constructor. Your help will be greatly appreciated.

Note: I've tested my code that uses the XmlSerializer using TextWriter
before using MemoryStream and it works perfectly.
 
J

Joe Feser

Try setting the MemoryStream.Position = 0

There is nothing to read if you keep the position at it's current location

Joe Feser
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top