C# 正在使用xmlDocument分析xml文件,但由于异常而分析失败。请在这方面帮助我
我正在尝试解析xml文件,但由于找不到output.dat文件而面临错误。 首先,我读取字符串中的所有xml代码,然后将其加载到xmlDocument对象中,但遇到了找不到output.dat的异常 这是我的密码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
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)