从特定节点循环 XML [英] Looping XML from a specific node
问题描述
我有一个 XML 文件,比如说
I have an XML File, say
<?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>
我正在存储上次执行的差异的版本(在本例中为 1.0.1).我不想在应用程序每次运行时都遍历整个 XML 文件,而是检查是否有任何新的差异(在这种情况下,我可以从上次差异中获取版本).
I am storing the version of the last diff that was executed (In this case 1.0.1). I don't want to loop through the entire XML file every time the application runs, instead, just checks if any new diffs (in this case I can get the version from the last diff).
我的问题是,我不想遍历整个 XML 来比较版本,以了解跳过哪些和执行哪些.
My problem is that, I don't want to loop through the entire XML to comparing versions, to know which ones to skip and which ones to execute.
目前,我正在遍历所有差异并比较版本,直到它获得更新的版本,它才会执行该版本,然后存储最后执行的差异.这是我的代码:
Currently, am looping through all diffs and comparing the versions, untill it gets a newer version, it executes that, then stores the last diff executed. Here is my code:
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
推荐答案
在寻找了一段时间并没有找到任何与我所寻找的类似的解决方案后,我尝试了几种方法,最终得到了解决.此解决方案 作者 dbasnett 对我解决这个问题很有帮助.
After Looking for a while and not finding any similar solutions to what I was looking for I tried a couple of methods and finally got it working. This Solution by dbasnett was instrumental to me fixing this problem.
首先我得到sqldiff.xml文件
First I get the sqldiff.xml file
Dim xe As System.Xml.Linq.XElement
xe = XElement.Load(IO.Path.Combine(yourpath, "sqldiff.xml"))
接下来我得到最后一个元素(感谢 dbasnett)
Next I get the Last element (Thanks to dbasnett)
Dim lastVersEL As XElement = xe...<version>.LastOrDefault
最后,我使用 lastVersEL
并将其与原始问题中的 baseVersion
进行比较,如果 lastVersEL
更大,则意味着有新的添加diffs,我使用下面的代码来获取传递版本的XElement
Lastly, I use lastVersEL
and compare it to baseVersion
from the Original Question, and if lastVersEL
is greater, it means there are new diffs added, and the I use the code below to get the XElement of the passed version
Dim lastExecutedEL As XElement = (From c In xe...<version> Where c.Value = baseVersion Select c).SingleOrDefault()
并获取索引(我正在寻找的解决方案)
And To get the index (THE SOLUTION I WAS LOOKING FOR)
Dim index As Integer = xe...<version>.Nodes.ToArray.ToList.IndexOf(lastExecutedEL.FirstNode)
然后我按照下面的代码继续循环并执行差异
And then I proceeded to loop and execute the diffs as per the code below
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
这篇关于从特定节点循环 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!