tags:

views:

126

answers:

2
 i will be passing the xml file like this:

         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'

+1  A: 

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

You might want to use the overload which takes an XmlReader (which is supported in 3.5).

EDIT: Sample code:

static XDocument LoadFromStream(Stream stream)
{
    using (XmlReader reader = XmlReader.Create(stream))
    {
        return XDocument.Load(reader);    
    }
}
Jon Skeet
yes i am using 3.5 framework. what should be the alternative to it?
Abu Hamzah
can you please show me some sample lines Stream using xmlReader?
Abu Hamzah
@teki: Edited to give a method you could use.
Jon Skeet
+2  A: 

The XDocument.Load(Stream) method is new in .NET 4. For earlier versions of the framework, you need to read the stream first and pass it in as a string:

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

    var document = XDocument.Parse(streamContents);
}
Lck
i am gettign error when it try to load the streamContents and teh error is: "Illegal characters in path.", my xml is pretty much very simple not special characters nothing.
Abu Hamzah
XDocument.Load takes a filename, not the XML itself. You *could* use XDocument.Parse instead - but the code in this answer currently assumes UTF-8... it would be more robust to use XmlReader.Create IMO.
Jon Skeet
Yup, fixed. Thanks
Lck