2010-05-01 152 views
8

我的代碼是有一個XML在第7行位置32解析錯誤,我真的不知道爲什麼XML解析錯誤 - C#

精確的錯誤轉儲

5/1/2010 10:21:42 AM 
System.Xml.XmlException: An error occurred while parsing EntityName. Line 7, position 32. 
    at System.Xml.XmlTextReaderImpl.Throw(Exception e) 
    at System.Xml.XmlTextReaderImpl.Throw(String res, Int32 lineNo, Int32 linePos) 
    at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos) 
    at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr) 
    at System.Xml.XmlTextReaderImpl.ParseAttributes() 
    at System.Xml.XmlTextReaderImpl.ParseElement() 
    at System.Xml.XmlTextReaderImpl.ParseElementContent() 
    at System.Xml.XmlTextReaderImpl.Read() 
    at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) 
    at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) 
    at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) 
    at System.Xml.XmlDocument.Load(XmlReader reader) 
    at System.Xml.XmlDocument.Load(String filename) 
    at Lookoa.LoadingPackages.LoadingPackages_Load(Object sender, EventArgs e) in C:\Users\Administrator\Documents\Visual Studio 2010\Projects\Lookoa\Lookoa\LoadingPackages.cs:line 30 

XML文件,請注意,這是隻是一個樣本,因爲我希望該計劃之前,我開始填補這個倉庫工作

<repo> 
<Packages> 
    <TheFirstPackage id="00001" longname="Mozilla Firefox" appver="3.6.3" pkgver="0.01" description="Mozilla Firefox is a free and open source web browser descended from the Mozilla Application Suite and managed by Mozilla Corporation. A Net Applications statistic put Firefox at 24.52% of the recorded usage share of web browsers as of March 2010[update], making it the second most popular browser in terms of current use worldwide after Microsoft's Internet Explorer." cat="WWW" rlsdate="4/8/10" pkgloc="http://google.com"/> 
</Packages> 
<categories> 
    <WWW longname="World Wide Web" description="Software that's focus is communication or primarily uses the web for any particular reason."> </WWW> 
    <Fun longname="Entertainment & Other" description="Music Players, Video Players, Games, or anything that doesn't fit in any of the other categories."> </Fun> 
    <Work longname="Productivity" description="Application's commonly used for occupational needs or, stuff you work on"> </Work> 
    <Advanced longname="System & Security" description="Applications that protect the computer from malware, clean the computer, and other utilities."> </Advanced> 
</categories> 
</repo> 

C#代碼的小部分

//Loading the Package and Category lists 
       //The info from them is gonna populate the listboxes for Category and Packages 
       Repository.Load("repo.info"); 
       XmlNodeList Categories = Repository.GetElementsByTagName("categories"); 
       foreach (XmlNode Category in Categories) 
       { 
        CategoryNumber++; 
        CategoryNames[CategoryNumber] = Category.Name; 
        MessageBox.Show(CategoryNames[CategoryNumber]); 
       } 

的Messagebox.Show()是隻是爲了確保它得到正確的結果

回答

18

&符號在XML特殊的意義。您需要更改:

Entertainment & Other 

在這個人物的地方使用character entity reference

Entertainment &amp; Other 

這同樣適用於System & Security

+2

在一天內更換,並確保當你在它正確轉義任何其它字符,以防萬一< or >單。 – ssube 2010-05-01 17:34:40

+0

...或者「在屬性值的某個位置上的附屬物」。 – 2010-05-01 19:05:01

2

您在xml'&'中有無效字符。

更新:與&amp;