2013-10-31 228 views
3

我正在製作一個Web應用程序,它將來自網站的RSS提要(URL位於數據庫中)加載到我的Web應用程序中。但我得到這個錯誤System.Xml.XmlException - 根元素丟失

System.Xml.XmlException:根元素丟失。缺失根元素 。在行:rssdoc.load(rssStream);

異常詳細信息:System.Xml.XmlException:有多個根元素。 2號線,2位所以如何通過單個XML元素

這裏封裝一切是我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Xml; 
using System.Net; 
using System.Text; 
using System.IO; 
using System.Data; 
using System.Data.SqlClient; 

public partial class poletics : System.Web.UI.Page 
{ 
    public SqlConnection oldbcon = new SqlConnection(); 
    static int n = 0; 
    static DataTable dt = new DataTable(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     oldbcon = opncon(); 

     using (oldbcon) 
     { 
      SqlDataAdapter ad = new SqlDataAdapter("SELECT * from LebPolRss", oldbcon); 
      ad.Fill(dt); 
     } 
     int f = 3; 
     while (n < f) 
     { 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       Literal feed = new Literal(); 
       try 
       { 
        feed.Text = ProcessRss(dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString()); 
       } 
       catch (WebException ex) 
       { 
        WebResponse response = ex.Response; 
       } 
       Panel1.Controls.Add(feed); 
      } 
      n++; 
     } 
    } 
    public static string ProcessRss(string rssUrl, string feed) 
    { 
     WebRequest request = WebRequest.Create(rssUrl); 
     WebResponse response = request.GetResponse(); 
     StringBuilder sb = new StringBuilder(""); 
     Stream rssStream = response.GetResponseStream(); 
     XmlDocument rssDoc = new XmlDocument(); 

     rssDoc.Load(rssStream);//here is the line where the exception thrown 
     XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item"); 

     string title = ""; 
     string link = ""; 
     string description = ""; 
     int upperlimit = rssItems.Count; 
     if (upperlimit > n) 
      upperlimit = n + 1; 
     if (upperlimit > 0) 
     { 
      sb.Append("<ul>"); 
      for (int i = 0; i < upperlimit - n; i++) 
      { 
       XmlNode rssDetail; 
       rssDetail = rssItems.Item(i + n).SelectSingleNode("title"); 
       if (rssDetail != null) 
       { 
        if (feed.ToString().Equals("tayyar")) 
        { 
         title = rssDetail.InnerText.Substring(5); 
        } 
        else 
        { 
         if (rssDetail.InnerText.Length > 75) 
         { 
          title = rssDetail.InnerText.Substring(0, 75); 
         } 
         else 
         { 
          title = rssDetail.InnerText; 
         } 
        } 
       } 
       else 
       { 
        title = ""; 
       } 

       rssDetail = rssItems.Item(i + n).SelectSingleNode("link"); 
       if (rssDetail != null) 
       { 
        if (feed.ToString().Equals("tayyar")) 
        { 
         if (rssDetail.InnerText.Substring(0, 21).CompareTo("http://www.tayyar.org") != 0) 
         { 
          link = "http://www.tayyar.org" + rssDetail.InnerText; 
         } 
         else 
         { 
          link = rssDetail.InnerText; 
         } 
        } 
        else 
        { 
         link = rssDetail.InnerText; 
        } 
       } 
       else 
       { 
        link = ""; 
       } 
       rssDetail = rssItems.Item(i + n).SelectSingleNode("description"); 
       if (rssDetail != null) 
       { 
        if (!rssDetail.InnerText.Substring(3, 3).Equals("Ad:")) 
        { 

         description = rssDetail.InnerText; 
        } 

       } 
       else 
       { 
        description = ""; 
       } 
       switch (feed) 
       { 
        case "tayyar": sb.Append("<div class='imgsep'><li><img src='logos/tayyar.jpg' width='50px' height='30px'/>&nbsp;&nbsp;&nbsp;<a href='" + link + "' target='_blank'>" + title + "</a>&nbsp;<img src='Images/smallarrow.png'/></li></div>"); 
         break; 
        case "14march": sb.Append("<div class='imgsep'><li><img src='logos/14march.jpg' width='50px' height='30px'/>&nbsp;&nbsp;&nbsp;<a href='" + link + "' target='_blank'>" + title + "</a>&nbsp;<img src='Images/smallarrow.png'/></li></div>"); 
         break; 
        case "annahar": sb.Append("<div class='imgsep'><li><img src='logos/annahar.jpg' width='50px' height='30px'/>&nbsp;&nbsp;&nbsp;<a href='" + link + "' target='_blank'>" + title + "</a>&nbsp;<img src='Images/smallarrow.png'/></li></div>"); 
         break; 
        case "alakhbar": sb.Append("<div class='imgsep'><li><img src='logos/akhbar.jpg' width='50px' height='30px'/>&nbsp;&nbsp;&nbsp;<a href='" + link + "' target='_blank'>" + title + "</a>&nbsp;<img src='Images/smallarrow.png'/></li></div>"); 
         break; 
        case "sadabeirut": sb.Append("<div class='imgsep'><li><img src='logos/echobeirut.png' width='50px' height='30px'/>&nbsp;&nbsp;&nbsp;<a href='" + link + "' target='_blank'>" + title + "</a>&nbsp;<img src='Images/smallarrow.png'/></li></div>"); 
         break; 
        case "assafir": sb.Append("<div class='imgsep'><li><img src='logos/assafir.png' width='50px' height='30px'/>&nbsp;&nbsp;&nbsp;<a href='" + link + "' target='_blank'>" + title + "</a>&nbsp;<img src='Images/smallarrow.png'/></li></div>"); 
         break; 
        case "aliwaa": sb.Append("<div class='imgsep'><li><img src='logos/aliwaa.jpg' width='50px' height='30px'/>&nbsp;&nbsp;&nbsp;<a href='" + link + "' target='_blank'>" + title + "</a>&nbsp;<img src='Images/smallarrow.png'/></li></div>"); 
         break; 
       } 
      } 
      sb.Append("</ul>"); 
     } 
     return sb.ToString(); 
    } 
    protected void LinkButton1_Click(object sender, EventArgs e) 
    { 
     oldbcon = opncon(); 
     using (oldbcon) 
     { 
      SqlDataAdapter ad = new SqlDataAdapter("SELECT * from LebPolRss", oldbcon); 
      ad.Fill(dt); 
     } 
     n = 4; 
     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      Literal feed = new Literal(); 
      try 
      { 
       feed.Text = ProcessRss(dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString()); 
      } 
      catch (WebException ex) { WebResponse response = ex.Response; } 
      Panel1.Controls.Add(feed); 
     } 
     LinkButton1.Visible = false; 
    } 
    public static SqlConnection opncon() 
    { 
     string connectionString = "Data Source=RAYYAN-THINK;Initial Catalog=newsProject;Integrated Security=True"; 
     SqlConnection conn = new SqlConnection(connectionString); 
     return conn; 
    } 
} 

堆棧跟蹤:

[XmlException: Root element is missing.] 
    System.Xml.XmlTextReaderImpl.Throw(Exception e) +69 
    System.Xml.XmlTextReaderImpl.ParseDocumentContent() +5589128 
    System.Xml.XmlTextReaderImpl.Read() +215 
    System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +145 
    System.Xml.XmlDocument.Load(XmlReader reader) +107 
    System.Xml.XmlDocument.Load(Stream inStream) +130 
    poletics.ProcessRss(String rssUrl, String feed) in c:\Users\RAYYAN\Documents\Visual Studio 2012\WebSites\WebSite1\poletics.aspx.cs:57 
    poletics.Page_Load(Object sender, EventArgs e) in c:\Users\RAYYAN\Documents\Visual Studio 2012\WebSites\WebSite1\poletics.aspx.cs:36 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 
    System.Web.UI.Control.OnLoad(EventArgs e) +92 
    System.Web.UI.Control.LoadRecursive() +54 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772 
+1

http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title –

+0

顯然這似乎是xml文件的問題。也許你可以發佈引發這個異常的XML? – user1567896

+0

這是太多的代碼。您應該使用您的判斷來僅發佈相關部分,如果可能的話發佈XML,並明確拋出異常的哪一行。 –

回答

5

錯誤解釋的最常見的情況例外:你的xml中缺少一個根元素。

意思是,你最有可能有幾個「根」元素,因此這些不是根元素。你需要一個xml元素來封裝其他的東西。這是要檢查的第一件事

+1

也許你可以添加一個小例子來說明它。 –

+0

以及它不是我的xml ..這是他們的XML,爲什麼有時它會變得完美,有時它拋出這個異常是有辦法處理這個異常,除了我怎麼可以封裝每個單一的XML元素的東西,請舉個例子。感謝您的幫助 – ra22

+0

使用try-catch來處理異常。在開始調整自己的軟件之前,您應該更好地解決問題的根源。檢查你爲什麼有時得到一個正確的XML,有時不。你不應該圍繞XML,它應該以這種方式交付 –