VB.net读取XML

时间:2016-11-01 07:35:31

标签: xml vb.net

有人可以用他的格式帮助我阅读 xml 中的值吗?

以下是 xml 示例。我需要得到以下内容:

>id = 4ebc-9c89
>employeeidnumber = 12345
>name = Smith, John
>weekday = 31/10/2016
>reason = VL

>weekday = 1/11/2016
>reason = VL

>id = 35bc-9c89
>employeeidnumber = 54321
>name = Smith, Jane
>weekday = 1/11/2016
>reason = VL*

如果员工申请两次或更多次休假,该计划应该获得他/她的全部假期。

<employeeleaverequest>
      <leaverequest>
        <id>4ebc-9c89</id>
        <employeeidnumber>12345</employeeidnumber>
        <name>Smith, John</name>
        <company name="ABC Company">42b8</company>    
        <position name="08">-d00077504</position>
        <dayleaverequest>
          <weekday date="31/10/2016">2</weekday>
          <segments>
            <segment>
              <id>ae2f2c</id>
              <hours>8</hours> 
              <reason name="Vacation Leave">VL</reason>
              <status>1</status>
            </segment>
          </segments>
        </dayleaverequest>
        <dayleaverequest>
          <weekday date="1/11/2016">3</weekday>
          <segments>
            <segment>
              <id>96898</id>
              <hours>8</hours>       
             <reason name="Vacation Leave">VL</reason>
             <status>1</status>
            </segment>
          </segments>
        </dayleaverequest>   
      </leaverequest>
      <leaverequest>
        <id>35bc-9c89</id>
        <employeeidnumber>54321</employeeidnumber>
        <name>Smith, Jane</name>
        <company name="ABC Company">42b8</company>    
        <position name="08">-d00077504</position>
        <dayleaverequest>
          <weekday date="1/11/2016">2</weekday>
          <segments>
            <segment>
              <id>ae333c</id>
              <hours>8</hours> 
              <reason name="Vacation Leave">VL</reason>
              <status>1</status>
            </segment>
          </segments>
        </dayleaverequest>    
      </leaverequest>
    </employeeleaverequest>

到目前为止,这是我的代码。

  For Each Node As XmlElement In nodelist
             strId = Node("id").InnerText
              strNumber = Node("employeeidnumber").InnerText
              strName = Node("name").InnerText

              Dim sLeaveDay As XmlNode = >Node.SelectSingleNode("dayleaverequest")
              If strLeaveDay IsNot Nothing Then
                  strLeaveDay = >sLeaveDay("weekday").Attributes.ItemOf("date").InnerText
              End If

              Dim sSegments As XmlNode = >Node.SelectSingleNode>("dayleaverequest/segments/segment")
              If sSegments IsNot Nothing Then
                  strReason = sSegments("reason").InnerText              
              End If

              MessageBox.Show(strId & "|" & strNumber & "|" & strName & >"|" & strLeaveDay & "|" & strReason)
          Next

2 个答案:

答案 0 :(得分:0)

试试xml linq:

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim doc As XDocument = XDocument.Load(FILENAME)

        Dim leaverequests = doc.Descendants("leaverequest").Select(Function(x) New With {
                    .id = x.Element("id"),
                    .employeeidnumber = x.Element("employeeidnumber"),
                    .name = x.Element("name"),
                    .company = x.Element("company"),
                    .postition = x.Element("position"),
                    .dayleaverequest = x.Elements("dayleaverequest").Select(Function(y) New With {
                        .weekday = y.Element("weekday").Value,
                        .date = y.Element("weekday").Attribute("date"),
                        .id = y.Descendants("id").FirstOrDefault(),
                        .hours = y.Descendants("hours").FirstOrDefault(),
                        .reason = y.Descendants("reason").Select(Function(z) CType(z.Value, String)).FirstOrDefault(),
                        .name = y.Descendants("reason").FirstOrDefault().Attribute("name").Value,
                        .status = y.Descendants("status").FirstOrDefault()
                    }).ToList()
                }).ToList()

    End Sub

End Module

答案 1 :(得分:0)

我能够解决自己的问题。

这是解决我问题的代码。我刚刚声明变量来阅读dayleaverequest。然后我用它上面的每个循环。

对于每个节点作为XmlElement在NodeList中strId = Node(“id”)。InnerText strNumber = Node(“employeeidnumber”)。InnerText strName = Node(“name”)。InnerText

Dim InnerNodeList As XmlNodeList = Node.SelectNodes("dayleaverequest")

'loop through InnerNodeList(dayleaverequest)
For Each InnerNode As XmlNode In InnerNodeList
    strLeaveDay = InnerNode("weekday").Attributes.ItemOf("date").InnerText

    Dim sSegments As XmlNode = Node.SelectSingleNode("dayleaverequest/segments/segment")
     If sSegments IsNot Nothing Then
      strReason = sSegments("reason").InnerText              
  End If

   MessageBox.Show(strId & "|" & strNumber & "|" & strName & >"|" & strLeaveDay & "|" & strReason)
Next

下一步