读取XML,节点数不同 [英] Read XML, different number of nodes

查看:90
本文介绍了读取XML,节点数不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在VB.NET(ASMX Web服务)中读取XML文件时,有时可能缺少某些节点. 我的代码如下:

When I am reading an XML file in VB.NET (ASMX webservice), on some occasion, some node may be missing. My code is the following:

nodetype = node("type").InnerText
nodetime = node("time").InnerText
nodefileName = node("fileName").InnerText

我已经考虑过这种情况,以查看该节点是否存在.如果不存在,则返回一个带有0的字符串.

And I've thought about this condition to see if the node exists or not. If it does not exist it returns a string with 0.

If node("fileName")Is Nothing Then
  nodefileName = "0"
Else
  nodefileName = nodefileName = node("fileName").InnerText.
End If

不必只对所有节点进行检查...您如何一次性对所有节点进行检查,如果XML文件中不存在该检查,则将0放入相应的变量中? 谢谢1000!

Instead of having to do the check for all nodes individually ... how could you do the check for all at once and if it doesn't exist in the XML file put 0 in the corresponding variable? Thanks 1000!

XML示例,XML并不总是具有所有节点.

XML sample, XML does not always have all nodes.

<?xml version="1.0" encoding="UTF-8"?>
<eventLog>
    <event>
        <type>access1</type>
        <fileName>file.xml</fileName>
        <time>2020-04-25</time>
        <baseExtraData>
            <sample>Bone</sample>
            <age>65</age>
        </baseExtraData>
    </event>
    <event>
        <type>access2</type>
        <fileName>file2.xml</fileName>
        <time>2020-04-24</time>
        <baseExtraData>
            <sample>Malow</sample>
            <age>11</age>
        </baseExtraData>
    </event>
</eventLog>

推荐答案

好,这是一个具有一个辅助功能的解决方案.在表单上添加了一个按钮"BtnImport"和一个名为"TxtXML"的文本框,其中包含您的xml文件的路径:

Well, here's a solution with one helper function. Added a button "BtnImport" on a form and a textbox named "TxtXML" which holds the path of your xml file:

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

Private Function GetNodeText(ByVal Dom As XmlDocument, ByVal Path As String) As String
    Dim Node As XmlNode = Dom.SelectSingleNode(Path)

    If Node Is Nothing Then Return vbNullString
    Return Node.InnerText
End Function

Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
    Dim DOC As New XmlDocument
    Dim SB As New StringBuilder
    Dim Line(4) As String

    SB.Append("type;fileName;time;sample;age")
    SB.Append(vbCrLf)

    DOC.Load(TxtXML.Text)

    Dim Counter As Integer = 1
    Dim EventPath = String.Format("/descendant::event[{0}]", Counter)
    Do Until DOC.SelectSingleNode(EventPath) Is Nothing
        Line(0) = GetNodeText(DOC, EventPath & "/type")
        Line(1) = GetNodeText(DOC, EventPath & "/fileName")
        Line(2) = GetNodeText(DOC, EventPath & "/time")
        Line(3) = GetNodeText(DOC, EventPath & "/baseExtraData/sample")
        Line(4) = GetNodeText(DOC, EventPath & "/baseExtraData/age")

        SB.Append(Join(Line, ";"))
        SB.Append(vbCrLf)

        Counter += 1
        EventPath = String.Format("/descendant::event[{0}]", Counter)
    Loop

    Dim FS As New FileStream(TESTFOLDER & "\Test.csv", FileMode.Create)
    Dim SW As New StreamWriter(FS)

    SW.Write(SB.ToString)
    SW.Close()
End Sub

这篇关于读取XML,节点数不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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