如何在VB.net中使用XMLReader解析XML [英] How to Parse XML using XMLReader in VB.net

查看:69
本文介绍了如何在VB.net中使用XMLReader解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个简单的代码示例,说明如何使用VB.net中的XMLReader读取以下XML。我已经能够阅读 topxml 部分和 DETAIL 部分,但无法获得 SUBDETAIL 部分项目。以下示例XML源由WEB服务返回。



我正在尝试从以下XML中读取以下内容, ID ,< b> TITLE , GROUPID 来自

< SUBDETAIL ID =A1B2TITLE =XYZ TitleGROUPID =2/> 





WEB服务返回XML:

<?xml version = 1.0encoding =UTF-8?> 
< topxml status =oktime =63>
< DETAIL ID =123457VAL1 =ABCVAL2 =DEF>
< SUBDETAIL ID =A1B2TITLE =XYZ TitleGROUPID =2/>
< / DETAIL>
< / topxml>



谢谢



ADDED 1/29/2014

谢谢Peter ..

当你的例子使用硬编码的XML作为xmlString变量并使用reader As XmlReader = XmlReader.Create时(新的StringReader(xmlString))。 但是,当读取从URL / WEB服务返回的XML时,reader.Value为Nothing / Empty。



2014年1月29日更新

看来当使用XMLReader时,返回的XML被读取(即reader.read),当我在VB.net中循环遍历XML时,返回的部分如下。



topxml

详情




XML的SUBDETAIL部分似乎从XMLReader 或我没有使用正确的调用来达到DETAIL的CHILD级别。



VB.net代码片段

  Dim  strMyUrl 作为 字符串 
' url
strMyUrl = http: //www.somplace.com/subfold/xyz/GetInfo.cfm?LOGInUName=JOHN&LINPassword=mypass&username=123457
Dim reader As XmlReader = XmlReader.Create(strMyUrl)
reader.ReadToFollowing( SUBDETAIL
reader.MoveToFirstAttribute()
Dim id As String = reader.Value
Debug.Print( SUBDETAIL ID: + id)



从URL / WEB服务返回的XML:



很抱歉有任何混淆,但XML如下, 从原始问题中错误地粘贴了CASE 。这是原始帖子中的XML,因为它来自WEB服务。下面的XML是在调用WEB服务URL时在浏览器中返回的格式。

但是当在我的vb.net代码中使用URL时,返回的XML似乎不显示有 SUBDETAIL 部分,或者我没有使用正确的调用因此我的POST的根,需要能够读取SUBDETAIL tems属性, ID TITLE GROUPID

 <?  xml     version   =   1.0    encoding  < span class =code-keyword> =  UTF-8  >  
< topxml status = < span class =code-keyword> ok time = 63 >
< DETAIL ID = 123457 VAL1 = ABC < span class =code-attribute> VAL2 = DEF >
< SUBDETAIL ID = A1B2 TITLE = XYZ标题 GROUPID = 2 / >
< / DETAIL >
< / topxml >





2014年1月30日更新



我试过使用以下内容而我仍然没有得到数据。

 reader.ReadToFollowing(  DETAIL 
reader.ReadToDescendant( SUBDETAIL



任何想要从XMLReader以外的XML读取所需值的想法都会受到高度赞赏,但我更喜欢将现有代码与XMLReader一起使用。谢谢!

注意:我可以从DETAIL部分读取所有属性,ID1,FNAME,LNAME,EMAIL。



我需要能够阅读 SUBDETAIL ID2 TITLE GROUPID ORGCODE



这是我接收的XML WEB服务。

 <?  xml     version   =  1.0    encoding   =  UTF-8    >  
< topxml status = 确定 时间 = 34 >
< DETAIL ID1 = 1234567 FNAME = John LNAME = Doe EMAIL = JDoe@Company.com >
< SUBDETAIL ID2 = A123-4567 TITLE = A Title Here GROUPID = 1 ORGCODE = ABC / >
< / DETAIL >
< / topxml >



谢谢

Ed

解决方案

引用:

任何想要从XMLReader以外的XML读取所需值的想法都将非常感激

我个人认为这种方法更容易。

 Private Sub TestXML()

Dim sbSource As New System.Text.StringBuilder(200)
with sbSource
.AppendLine(<? xml 版本 = 1.0< /温泉n> encoding = UTF-8? >
.AppendLine(< topxml status = < span class =code-attribute> OK
time = 34 >
.AppendLine(< DETAIL ID1 = 1234567 FNAME = John LNAME = Doe EMAIL = JDoe@Company.com >
.AppendLine(< SUBDETAIL ID2 = A123- 4567 TITLE = A 标题 此处 GROUPID = 1 ORGCODE = ABC/ >
.AppendLine(< /详情 >
.AppendLine(< DETAIL ID1 = 1234568 FNAME = LNAME = Doe EMAIL = JnDoe@Company.com >
.AppendLine(< SUBDETAIL < span class =code-attribute> ID2
= A123-4562 TITLE = A 标题 这里 < span class =code-attribute> GROUPID = 1 ORGCODE = ABC/ >
.AppendLine(< / DETAIL >
.AppendLine(< / topxml >


结束strMyUrl As String =http://www.somplace.com/subfold/xyz/GetInfo.cfm?LOGInUName=JOHN&LINPassword=mypass&

Dim doc As New XmlDocument
doc.LoadXml(sbSource.ToString())'删除这个
'doc.Load(strMyUrl)'并使用此行从url加载

For Each details As XmlElement in doc.DocumentElement.GetElementsByTagName(DETAIL)
For each subdetail As XmlElement In detail.GetElementsByTagName(SUBDETAIL)
Dim FName As String = GetAttibuteValue(detail,FNAME)
Dim LName As String = GetAttibuteValue(detail,LNAME)
Dim id2 As String = GetAttibuteValue(subdetail,ID2)
Dim title As String = GetAttibuteValue(subdetail,TITLE)
Dim groupid As String = GetAttibuteValue(subdetail,GROUPID)
Dim orgcode As String = GetAttibuteValue(subdetail,ORGCODE)
Next subdetail
下一个细节
End Sub'Test

私有函数GetAttibuteValue(ByVal节点为XmlNode,ByVal attibutename为String)As String
Dim ret As String = String.Empty
如果节点IsNot Nothin g AndAnd node.Attributes IsNot Nothing然后
Dim attrib作为XmlNode = node.Attributes.GetNamedItem(attibutename)
如果attrib IsNot Nothing那么
ret = attrib.Value
结束如果
结束如果
返回ret
结束函数'GetAttibuteValue


从我的示例代码改编:

  Imports  System.Xml 
Imports System.IO
Imports System.Text

Public Class Form1

私有 Sub Form1_Load ( ByVal sender As System。 Object ByVal e As System.EventArgs)句柄 MyBase .Load

Dim xmlString < span class =code-keyword> As String = < topxml status ='ok'time ='63'>& _
< DETAIL ID ='123457'VAL1 ='ABC'VAL2 ='DEF'> & _
< SUBDETAIL ID ='A1B2'TITLE ='XYZ Title'GROUPID ='2'/ >& _
< / DETAIL>& _
< / topxml>

' 创建XmlReader
使用阅读器 As XmlReader = XmlReader.Create( New StringReader(xmlString))

reader .ReadToFollowing( SUBDETAIL
reader.MoveToFirstAttribute()
Dim id As String = reader.Value
TextBox1.Text = SUBDETAIL ID: + id

reader .MoveToNextAttribute()
Dim title As 字符串 = reader.Value
TextBox2.Text = SUBDETAIL标题: + title


结束 使用

结束 Sub
结束


尝试:

 reader.ReadToFollowing(  DETAIL
reader.ReadToDescendant( SUBDETAIL


I need a simple code example of how to read through the following XML using XMLReader in VB.net. I have been able to read the topxml section and the DETAIL section but have been unable to get the SUBDETAIL section items. The sample XML source below is returned by a WEB service.

I am trying to read the following from the below XML, ID, TITLE, and GROUPID from

<SUBDETAIL ID="A1B2" TITLE="XYZ Title" GROUPID="2"/> 



WEB Service Returned XML:

 <?xml version="1.0" encoding="UTF-8" ?>
<topxml status="ok" time="63">
   <DETAIL ID="123457" VAL1="ABC" VAL2="DEF">
     <SUBDETAIL ID="A1B2" TITLE="XYZ Title" GROUPID="2"/>
   </ DETAIL>
 </ topxml>


Thanks

ADDED 1/29/2014
Thanks Peter..
This works when using your example with hard coded XML for the xmlString variable and Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString)). However when reading the XML returned from the URL/WEB service the reader.Value is Nothing/Empty.

UPDATED 1/29/2014
It appears that when using XMLReader that the returned XML is read in pieces (i.e. reader.read ) and that when I loop through the XML in VB.net the returned sections are as follows.

topxml
DETAIL


The SUBDETAIL part of the XML appears to be missing from the XMLReader or I am not using the correct call to get to the CHILD level of the DETAIL.

VB.net Code snippet

Dim strMyUrl As String
' url
strMyUrl = "http://www.somplace.com/subfold/xyz/GetInfo.cfm?LOGInUName=JOHN&LINPassword=mypass&username=123457"
Dim reader As XmlReader = XmlReader.Create(strMyUrl)
    reader.ReadToFollowing("SUBDETAIL")
    reader.MoveToFirstAttribute()
    Dim id As String = reader.Value
    Debug.Print("SUBDETAIL ID: " + id)


XML returned from the URL/WEB Service:

Sorry for any confusion but the XML is as follows and the CASE was incorrectly pasted from the orginal question.. Here is the XML from the original post as it comes from the WEB service. The below XML is of the format that is returned in the browser when calling the WEB service URL.
However when the URL is used in my vb.net code the returned XML does not appear to have the SUBDETAIL section, or I am not using the correct calls thus the root of my POST, needing to be able to read the SUBDETAIL tems attributes, ID, TITLE, GROUPID.

<?xml version="1.0" encoding="UTF-8"?>
<topxml status="ok" time="63">
   <DETAIL ID="123457" VAL1="ABC" VAL2="DEF">
     <SUBDETAIL ID="A1B2" TITLE="XYZ Title" GROUPID="2"/>
   </ DETAIL>
 </ topxml>



UPDATED 1/30/2014

I have tried to use the following and I am still not getting the data.

reader.ReadToFollowing("DETAIL")
reader.ReadToDescendant("SUBDETAIL")


Any ideas that are simpler to read the required values from the XML other than XMLReader would be greatly appreciated but would prefer the ability to use my existing code with XMLReader. Thanks!
NOTE: I am able to read ALL attributes from the DETAIL section, ID1, FNAME, LNAME, EMAIL.

I need to be able to read the SUBDETAIL values ID2, TITLE, GROUPID, ORGCODE

Here is the XML that I am receivig from the WEB service.

<?xml version="1.0" encoding="UTF-8" ?>
    <topxml status="OK" time="34">
        <DETAIL ID1="1234567" FNAME="John" LNAME="Doe" EMAIL="JDoe@Company.com">
            <SUBDETAIL ID2="A123-4567" TITLE="A Title Here" GROUPID="1" ORGCODE="ABC" />
        </DETAIL>
    </topxml>


Thanks
Ed

解决方案

Quote:

Any ideas that are simpler to read the required values from the XML other than XMLReader would be greatly appreciated

Personally, I find this approach easier.

Private Sub TestXML()

 Dim sbSource As New System.Text.StringBuilder(200)
 With sbSource
   .AppendLine("<?xml version=""1.0"" encoding=""UTF-8""?>")
   .AppendLine("<topxml status=""OK"" time=""34"">")
   .AppendLine("<DETAIL ID1=""1234567"" FNAME=""John"" LNAME=""Doe"" EMAIL=""JDoe@Company.com"">")
   .AppendLine("    <SUBDETAIL ID2=""A123-4567"" TITLE=""A Title Here"" GROUPID=""1"" ORGCODE=""ABC""/>")
   .AppendLine("</DETAIL>")
   .AppendLine("<DETAIL ID1=""1234568"" FNAME=""Jane"" LNAME=""Doe"" EMAIL=""JnDoe@Company.com"">")
   .AppendLine("    <SUBDETAIL ID2=""A123-4562"" TITLE=""A Title Here"" GROUPID=""1"" ORGCODE=""ABC""/>")
   .AppendLine("</DETAIL>")
   .AppendLine("</topxml>")
 End With

Dim strMyUrl As String = "http://www.somplace.com/subfold/xyz/GetInfo.cfm?LOGInUName=JOHN&LINPassword=mypass&"

Dim doc As New XmlDocument
doc.LoadXml(sbSource.ToString()) ' delete this
'doc.Load(strMyUrl)              ' and use this line to load from url

For Each detail As XmlElement In doc.DocumentElement.GetElementsByTagName("DETAIL")
   For Each subdetail As XmlElement In detail.GetElementsByTagName("SUBDETAIL")
      Dim FName As String = GetAttibuteValue(detail, "FNAME")
      Dim LName As String = GetAttibuteValue(detail, "LNAME")
      Dim id2 As String = GetAttibuteValue(subdetail, "ID2")
      Dim title As String = GetAttibuteValue(subdetail, "TITLE")
      Dim groupid As String = GetAttibuteValue(subdetail, "GROUPID")
      Dim orgcode As String = GetAttibuteValue(subdetail, "ORGCODE")
   Next subdetail
Next detail
End Sub ' Test

Private Function GetAttibuteValue(ByVal node As XmlNode, ByVal attibutename As String) As String
   Dim ret As String = String.Empty
   If node IsNot Nothing AndAlso node.Attributes IsNot Nothing Then
      Dim attrib As XmlNode = node.Attributes.GetNamedItem(attibutename)
      If attrib IsNot Nothing Then
         ret = attrib.Value
      End If
   End If
   Return ret
End Function 'GetAttibuteValue


Adapt from my sample code below:

Imports System.Xml
Imports System.IO
Imports System.Text

Public Class Form1

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

        Dim xmlString As String = "<topxml status='ok' time='63'>" & _
        "<DETAIL ID='123457' VAL1='ABC' VAL2='DEF'>" & _
                        "<SUBDETAIL ID='A1B2' TITLE='XYZ Title' GROUPID='2'/> " & _
        "</ DETAIL>" & _
        "</ topxml>"

        ' Create an XmlReader
        Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))

            reader.ReadToFollowing("SUBDETAIL")
            reader.MoveToFirstAttribute()
            Dim id As String = reader.Value
            TextBox1.Text = "SUBDETAIL ID: " + id

            reader.MoveToNextAttribute()
            Dim title As String = reader.Value
            TextBox2.Text = "SUBDETAIL TITLE: " + title


        End Using

    End Sub
End Class


Try:

reader.ReadToFollowing("DETAIL")
reader.ReadToDescendant("SUBDETAIL")


这篇关于如何在VB.net中使用XMLReader解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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