0

以下是我的完整代码。我想要做的是随机读取 Button10_Click 上的标签的 XML 资源文件行,但程序没有从嵌入的 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'eIZi@</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>
4

1 回答 1

1

您需要调用XDocument.Load来加载清单流:

Dim myXml = XDocument.Load(var)
var.Close()
于 2013-02-10T18:59:30.043 回答