2013-11-27 26 views
0

我想解析一個XML文件但面對錯誤作爲output.dat文件找不到。 首先,我在一個字符串讀取所有的XML代碼,然後將其加載到output.dat沒有找到 這裏XmlDocument對象,但臉上的例外是我的代碼使用xmlDocument解析xml文件但由於異常而無法解析。請幫我在這方面

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); 
     } 
    } 
} 

}

請告訴我哪裏是錯誤

+0

當我調試我的代碼時xml變量包含output.xml中的所有xml,但在load方法中獲得了execption – Mujadid

+0

爲什麼不直接使用'Load'方法直接讀取文件?什麼是確切的例外信息?通常它會指出文件有什麼問題。 –

+0

LoadXml用於從文件加載。你想要Pooli在下面說的。 – pquest

回答

2

考慮更換

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

隨着

XDocument xml = XDocument.Parse(xml)