2016-11-01 198 views
1

有人能幫助我如何讀取xml中的值與他的格式?以下是xml樣本。我需要得到以下內容:VB.net閱讀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 
+0

您應該添加什麼是當前代碼的問題,一個錯誤(哪個消息),一個意外的行爲(哪一個)。也就是說,如果代碼完全**,如下所示,我懷疑那些散佈在代碼中的字符散佈在問題中 – Sehnsucht

+0

上面的代碼只是爲每個人選擇第一個假。我想循環dayleaverequest,以便它會輸出多個假如果有多個假期。我不知道那些來自哪裏。我的代碼沒有這些字符。 – Marlofs

回答

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

不幸的是,由於某種原因,欣賞@jdweng。我們更喜歡使用我發佈的代碼。 – Marlofs

+0

Xml linq是一個增強的Microsoft Net Library,它具有更好的查詢數組xml文件的能力。 – jdweng

+1

@jdweng不是什麼大不了的,但你可以使用Xml Literals來減輕可讀性並簡化代碼 – Sehnsucht

0

我能解決我自己的問題。

這是解決我的問題的代碼。我只是聲明變量來讀取dayleaverequest。然後我使用它的每個循環。

對於每個節點的XmlElement在節點列表strId =節點( 「ID」)。的innerText strNumber =節點( 「employeeidnumber」)。的innerText則strName =節點( 「名稱」)。的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 

下一頁