我有一个XML文件,比如说
<?xml version="1.0" encoding="UTF-8"?>
<sqldiff version="1.0">
<diff>
<version>1.0.0</version>
<sql>
CODE HERE
</sql>
</diff>
<diff>
<version>1.0.1</version>
<sql>
CODE HERE
</sql>
</diff>
</sqldiff>我存储了上次执行的diff的版本(在本例中为1.0.1)。我不想在应用程序每次运行时遍历整个XML文件,而是只检查是否有新的差异(在本例中,我可以从最后一个差异中获得版本)。
我的问题是,我不想遍历整个XML来比较版本,知道跳过哪些版本和执行哪些版本。
目前,我循环所有的差异和比较版本,直到它得到一个较新的版本,它执行它,然后存储最后执行的比较。下面是我的代码:
Dim BaseVersion = New Version(GetLastVersion()) 'Eg. returns 1.0.2
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("D:\sqldiff.xml")
Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/sqldiff/diff")
Dim pID As String = "", pCode As String = ""
For Each node As XmlNode In nodes
pID = node.SelectSingleNode("version").InnerText
pCode = node.SelectSingleNode("sql").InnerText
'Checks if pID>BaseVersion Then Executes code and store current pID
'Else Continue
Next发布于 2017-02-20 12:55:36
在寻找了一段时间后,我没有找到任何与我所寻找的类似的解决方案,我尝试了几种方法,最终使其工作。dbasnett的This Solution对我解决这个问题很有帮助。
首先,我获取sqldiff.xml文件
Dim xe As System.Xml.Linq.XElement
xe = XElement.Load(IO.Path.Combine(yourpath, "sqldiff.xml"))接下来,我得到了最后一个元素(感谢dbasnett)
Dim lastVersEL As XElement = xe...<version>.LastOrDefault最后,我使用lastVersEL并将其与原始问题中的baseVersion进行比较,如果lastVersEL更大,则意味着添加了新的差异,并且我使用以下代码来获取传递的版本的XElement
Dim lastExecutedEL As XElement = (From c In xe...<version> Where c.Value = baseVersion Select c).SingleOrDefault()并获得索引(我正在寻找的解决方案)
Dim index As Integer = xe...<version>.Nodes.ToArray.ToList.IndexOf(lastExecutedEL.FirstNode)然后,我按照下面的代码进行循环并执行diffs
If lastVersEL > baseVersion Then
Dim lastExecutedEL As XElement = (From c In xe...<version> Where c.Value = baseVersion Select c).SingleOrDefault()
Dim index As Integer = xe...<version>.Nodes.ToArray.ToList.IndexOf(lastExecutedEL.FirstNode)
For i As Integer = index To xe...<version>.Nodes.Count
'Executes the SQL stored in the Elements by gettings the value as below
'elements(i).Parent.<sql>.Value.Trim
Next
End If发布于 2017-01-08 19:50:34
你的问题有点令人困惑,因为xml似乎与提供的代码不匹配,即代码中对Product_name的引用。
就获取最新版本节点并检查新版本而言,这样的操作应该是有效的,
Public Function NewDiff(newVers As String) As String
'Dim yourpath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
'yourpath = IO.Path.Combine(yourpath, "test.xml")
Dim xe As XElement
' to load from a file
'xe = XElement.Load(yourpath)
' for testing
xe = <sqldiff version="1.0">
<diff>
<version>1.0.0</version>
<sql>
<!-- CODE HERE -->
foo
</sql>
</diff>
<diff>
<version>1.0.1</version>
<sql>
<!-- CODE HERE -->
bar
</sql>
</diff>
</sqldiff>
Dim rv As String = Nothing
Dim lastVersEL As XElement = xe...<version>.LastOrDefault
If lastVersEL.Value <> newVers Then
'get the sql nodes value for the selected version
rv = lastVersEL.Parent.<sql>.Value.Trim
Stop
End If
' to save file
' xe.Save(yourpath)
Return rv
End Function测试如下
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = NewDiff("1.0.2")
End Sub如果您需要检查新版本是否存在于xml中,因为新版本可能没有按顺序传递,那么需要进行一些小的更改。
https://stackoverflow.com/questions/41531837
复制相似问题