如何使用我的 vb 2010 脚本读取此 Xml 文件 [英] How do I read this Xml file with my vb 2010 script

查看:47
本文介绍了如何使用我的 vb 2010 脚本读取此 Xml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里得到了我的 xml 文件.该文件名为 MyName.xml

so i got my xml file here. the file is called MyName.xml

<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<Data>
  <Person>
    <Name>ggg</Name>
    <Email>erty</Email>
    <Tel>567890-</Tel>
  </Person>
  <Person>
    <Name>hank</Name>
    <Email>3456</Email>
    <Tel>234567890-</Tel>
  </Person>
</Data>

我不知道如何让这个函数读取这个xml文件.我觉得我快要做到这一点了.我从书中复制了代码,然后我更改了一些部分.我只是不知道如何让它改变以便我使用它.我对这里的一些东西也很困惑.我留下了评论.但我想知道我错过了什么错误不断弹出.

I don't know how to get this function to read this xml file. I feel like I am close to getting this right. I copied the code from the book, then I changed some parts. I just don't know how to get it to change for me to use it. I am also very confused on some stuff here. I left comments on it. But i would like to know what am i missing for the error to keep popping up.

错误 - 未定义类型人".(第 8、10、20 行)

Error - Type 'Person' is not defined. (lines 8, 10, 20)

对于拥有同一本书(murach 的 Visual Basic 2010)的任何人,我更改之前的原始代码位于第 713 页

Also for anyone who has the same book (murach's Visual Basic 2010) the original code before I changed it is on page 713

书中的原始代码使用了一个包含 4 个条目的 xml 文件.每个都有 3 个部分,很像我的.

The original code from the book used a xml file that had 4 entries. Each with 3 parts, a lot like mine.

Imports System.Xml
Imports System.IO

Public Class Form2

    Private Const path As String = "MyName.xml" 'it is in the bin folder.'

    Public Shared Function GetPerson() As List(Of Person) 'what does this do? (Of Person) Am i missing something?'

        Dim people As New List(Of Person) 'Here it is again. What do i do to fix this partpart'

        Dim settings As New XmlReaderSettings
        settings.IgnoreComments = True
        settings.IgnoreWhitespace = True

        Dim xmlIn As XmlReader = XmlReader.Create(path, settings)

        If xmlIn.ReadToDescendant("Person") Then
            Do
                Dim person As New Person
                person.Name = xmlIn("Name")
                xmlIn.ReadStartElement("Person")
                person.Email = xmlIn.ReadElementContentAsString
                person.Tel = xmlIn.ReadElementContentAsString     'to make life simple for testing this, for now.'
                people.Add(person)
            Loop While xmlIn.ReadToNextSibling("Person")
        End If

        xmlIn.Close()
        Return people

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'I will end up puting code here to do something later. Just trying stuff right now.' 


    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub
End Class

任何帮助都会很好.我知道有更好的方法,也许更简单,但我需要通过我正在上的这门课.所以我得到的只是这个代码和一本非常大的书.稍后对于实际分配,我必须将其添加到 vb 中的数组中,然后搜索它并 bla bla bal .... 所以这只是我理解代码,所以我可以进入那部分.感谢所有可以帮助我的人.

Any help would be nice. I know there is a better way and maybe even easier but i need to to pass this class i am taking. So all i got is this code and a really big book. Later for the actual assignment i have to add this to an array in vb and then search it and bla bla bal.... so this is just me understanding the code so i can get to that part. Thanks to any and all that can help me.

推荐答案

你缺少的是 type person"——代码需要它才能工作.一个简单的实现可能如下所示:

What you are missing is the type "person" - which the code requires to work. A simple implementation could look like:

Public Class Person
   Public Name As String
   Public Tel As String
   Public EMail As String
End Class

这篇关于如何使用我的 vb 2010 脚本读取此 Xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆