How to read an XML file using System.IO.Stream with LINQ

I will transfer the xml file as follows:

File1.PostedFile.InputStream 

//reading xml file.....
public static void readXMLOutput(Stream stream)
{

    System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Load(stream);

    var query = from p in xml.Element("ste").Element("Application")
                //where (int)p.Element("Id") == 1
                select Page;

    foreach (var record in query)
    {
        Response.Write("dfe") + record.Element("dfe").Value;
    }

Error:

Error 1 The best overloaded method match for 'System.Xml.Linq.XDocument.Load (string)' has some invalid arguments

cannot convert from 'System.IO.Stream' to 'string'

+5
source share
2 answers

Are you using .NET 3.5 by accident? XDocument.Load(Stream)apparently only in .NET 4.

You might want to use an overload that acceptsXmlReader (which is supported in 3.5).

EDIT: Code Example:

static XDocument LoadFromStream(Stream stream)
{
    using (XmlReader reader = XmlReader.Create(stream))
    {
        return XDocument.Load(reader);    
    }
}
+12
source

XDocument.Load(Stream) .NET 4. :

public static void readXMLOutput(Stream stream){
    string streamContents;
    using(var sr = new StreamReader(stream)){
        streamContents = sr.ReadToEnd();
    }

    var document = XDocument.Parse(streamContents);
}
+3

All Articles