0
以下是我的全部代碼。我想要做的是獲取一個隨機的XML資源文件讀取到Button10_Click上的標籤,但該程序沒有識別來自嵌入式XML文件的任何信息,資源流是否在某處丟失?在Visual Studio 2010中無法加載XML嵌入式資源?
Imports System.IO
Imports System
Imports System.Reflection
Imports System.Xml
Imports System.Security.Permissions
Public Class Form1
Dim asm = Assembly.GetExecutingAssembly()
Dim var = asm.GetManifestResourceStream("WindowsApplication2.british-english-dictionary.xml")
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
Dim rand = New Random()
Dim myXml = New XDocument()
Dim lexemeList = myXml.Descendants("lexeme").ToList()
Dim randomLexeme = lexemeList(rand.Next(0, lexemeList.Count - 1))
Label1.Text = randomLexeme
End Sub
End Class
編輯:XML文件的示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<lexeme><grapheme>A</grapheme> <phoneme>'eI</phoneme></lexeme>
<lexeme><grapheme>A'asia</grapheme> <phoneme>a'[email protected]</phoneme></lexeme>
<lexeme><grapheme>A's</grapheme> <phoneme>'eIz</phoneme></lexeme>
<lexeme><grapheme>AOL</grapheme> <phoneme>'eI0l</phoneme></lexeme>
<lexeme><grapheme>AOL's</grapheme> <phoneme>'eI0lz</phoneme></lexeme>
<lexeme><grapheme>Aaberg</grapheme> <phoneme>'A:b3:g</phoneme></lexeme>
<lexeme><grapheme>Aaberg's</grapheme> <phoneme>'A:b3:gz</phoneme></lexeme>
</root>
我已經試過與ammendment運行它你建議但不幸的是'Dim randomLexeme = lexemeList(rand.Next(0,lexemeList.Count - 1))'仍然超出界限,因爲lexemeList.Count = 0,可能是因爲Xml沒有正確加載。 – Andy 2013-02-10 19:12:18
@Andy你有沒有檢查文檔是否已經加載到myXaml中?嘗試更改加載選項'myXml.Load(var,LoadOptions.PreserveWhitespace)' – AbZy 2013-02-10 19:21:47
我不知道如何檢查它是否加載到myXaml中;有沒有簡單的方法來檢查?修改'myXml.Load(var,LoadOptions.PreserveWhitespace)'仍然會出現該錯誤。 – Andy 2013-02-10 19:32:08