从XML XElement变量中检索字符串

时间:2016-05-25 14:44:56

标签: xml vb.net linq

我有以下代码查询Logfile条目并检索XML数据并将其存储为XElement变量。

logReader = New EventLogReader(eventsQuery)
Dim eventInstance As EventRecord = logReader.ReadEvent()

While Not eventInstance Is Nothing
    Dim eventXml As String = eventInstance.ToXml()
    Dim xmlElem As XElement = XElement.Parse(eventXml)

我的xmlElem变量包含以下数据:

<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'>
    <System>
        <Provider Name='Microsoft-Windows-Backup' Guid='{1DB28F2E-8F80-4027-8C5A-A11F7F10F62D}'/>
        <EventID>99</EventID>
        <Version>1</Version>
        <Level>4</Level>
        <Task>0</Task>
        <Opcode>0</Opcode>
        <Keywords>0x4000000000000000</Keywords>
        <TimeCreated SystemTime='2016-05-10T20:40:49.791707000Z'/>
        <EventRecordID>1</EventRecordID>
        <Correlation/>
        <Execution ProcessID='1748' ThreadID='3240'/>
        <Channel>Microsoft-Windows-Backup</Channel>
        <Computer>LAB</Computer>
        <Security UserID='S-1-5-18'/>
    </System>
    .......
</Event>

我正在尝试检索<Version>标记之间的数字。这只会在变量中存在一次。

我在这里和其他地方经历了很多帖子,但是我无法让这些其他解决方案起作用,或者根本就不理解。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

使用LINQ to XMLXML literals

' At the top of the file
Imports <xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
...
While Not eventInstance Is Nothing
    Dim eventXml As String = eventInstance.ToXml()
    Dim xmlElem As XElement = XElement.Parse(eventXml)
    Dim version = xmlElem.<System>.<Version>.Single().Value