XML 解析错误 - C#

发布于 2024-08-30 15:17:46 字数 3281 浏览 0 评论 0原文

我的代码在第 7 行位置 32 处出现 XML 解析错误,我不太确定为什么

精确错误转储

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() 只是为了确保它获得正确的结果

My code is having an XML parsing error at line 7 position 32 and I'm not really sure why

Exact Error Dump

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 File, please note this is just a sample because I want the program to work before I begin to fill this repository

<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>

Small part of C# Code

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

The Messagebox.Show() is just to make sure it's getting the correct results

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉默的熊 2024-09-06 15:17:46

& 符号在 XML 中具有特殊含义。您需要更改:

Entertainment & Other

使用 字符实体引用 代替此字符:

Entertainment & Other

同样适用到系统&安全性。

The ampersand has a special meaning in XML. You need to change:

Entertainment & Other

to use a character entity reference in the place of this character:

Entertainment & Other

The same applies to System & Security.

我们只是彼此的过ke 2024-09-06 15:17:46

您的 xml '&' 中有无效字符。

更新:将其替换为 &

You have invalid character in your xml '&'.

Updated: Replace it with &

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文