Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在使用xmlDocument分析xml文件,但由于异常而分析失败。请在这方面帮助我_C#_Xml - Fatal编程技术网

C# 正在使用xmlDocument分析xml文件,但由于异常而分析失败。请在这方面帮助我

C# 正在使用xmlDocument分析xml文件,但由于异常而分析失败。请在这方面帮助我,c#,xml,C#,Xml,我正在尝试解析xml文件,但由于找不到output.dat文件而面临错误。 首先,我读取字符串中的所有xml代码,然后将其加载到xmlDocument对象中,但遇到了找不到output.dat的异常 这是我的密码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using Sys

我正在尝试解析xml文件,但由于找不到output.dat文件而面临错误。 首先,我读取字符串中的所有xml代码,然后将其加载到xmlDocument对象中,但遇到了找不到output.dat的异常 这是我的密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using System.IO;
namespace XML
{
    class Program
{
     static void Main(string[] args)
     {
        try
        {
            StreamReader sr = new StreamReader("output.xml");
            String xml = "";
            String line = sr.ReadLine();
            while (line != null)
            {
                xml += line;
                line = sr.ReadLine();
            }
            sr.Close();
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(xml);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
}

请告诉我错误在哪里

请考虑更换

   XmlDocument xdoc = new XmlDocument();
   xdoc.LoadXml(xml);


当我调试代码xml变量时,它包含output.xml中的所有xml,但在load方法中得到了execption为什么不直接使用
load
方法读取文件?确切的异常消息是什么?通常,它会指出文件的错误。LoadXml用于从文件加载。你想要我在下面说的话。
XDocument xml = XDocument.Parse(xml)