【问题标题】:Read XML, different number of nodes读取 XML,不同数量的节点
【发布时间】:2020-08-09 10:54:03
【问题描述】:

当我在 VB.NET (ASMX webservice) 中读取 XML 文件时,有时,某些节点可能会丢失。 我的代码如下:

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

我已经考虑过这个条件来查看节点是否存在。如果不存在,则返回一个带 0 的字符串。

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

不必单独检查所有节点...如何一次检查所有节点,如果 XML 文件中不存在,则将 0 放入相应的变量中? 谢谢1000!

编辑: XML 示例,XML 并不总是包含所有节点。

<?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>

【问题讨论】:

  • 需要查看 xml 文件示例来帮助。
  • 准备好了,对不起。我编辑帖子以放置 XML 的示例。

标签: xml vb.net asmx


【解决方案1】:

使用 Xml Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var events = doc.Descendants("event").Select(x => new {
                type = (string)x.Element("type"),
                filename = (string)x.Element("fileName"),
                time = (DateTime)x.Element("time")
            }).ToList();
        }
    }
}

【讨论】:

    【解决方案2】:

    嗯,这是一个带有一个辅助函数的解决方案。在表单上添加了一个按钮“BtnImport”和一个名为“TxtXML”的文本框,其中包含您的 xml 文件的路径:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      相关资源
      最近更新 更多