如何使用带有LINQ的System.IO.Stream读取XML文件

Nic*_*ahn 5 c# xml linq asp.net

我将像这样传递xml文件:

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;
    }
Run Code Online (Sandbox Code Playgroud)

错误:

错误1'System.Xml.Linq.XDocument.Load(string)'的最佳重载方法匹配有一些无效的参数

无法从'System.IO.Stream'转换为'string'

Jon*_*eet 14

您是否有机会使用.NET 3.5?XDocument.Load(Stream)显然只是到了.NET 4.

您可能希望使用带有XmlReader(在3.5中支持)的重载.

编辑:示例代码:

static XDocument LoadFromStream(Stream stream)
{
    using (XmlReader reader = XmlReader.Create(stream))
    {
        return XDocument.Load(reader);    
    }
}
Run Code Online (Sandbox Code Playgroud)