简体   繁体   English

VB.net读取XML

[英]VB.net read XML

Can someone help me how to read values in xml with his format? 有人可以帮助我如何用他的格式读取xml中的值吗?

Below is the sample xml . 下面是示例xml I need to get the following: 我需要得到以下内容:

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

If employee applied for two or more leave, the program should get all his/her leaves. 如果员工申请两次或更多次休假,该计划应该得到他/她的全部假期。

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

Here's my code so far. 到目前为止,这是我的代码。

  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

Try xml linq : 试试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

I was able to resolved my own problem. 我能够解决自己的问题。

This is the code that solved my problem. 这是解决我的问题的代码。 I just declared variable to read the dayleaverequest. 我刚刚声明变量来阅读dayleaverequest。 Then I use for each loop on it. 然后我用它上面的每个循环。

For Each Node As XmlElement In NodeList strId = Node("id").InnerText strNumber = Node("employeeidnumber").InnerText strName = Node("name").InnerText 对于每个节点作为节点列表中的XmlElement strId =节点(“id”)。InnerText strNumber =节点(“employeeidnumber”)。InnerText strName =节点(“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

Next 下一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM