我需要一點幫助來簡化作業,如果可能的話,不要手動完成。
我有一個字串串列:
<title>TITLE 1</title>
<title>TITLE 2</title>
<title>TITLE 3</title>
<title>TITLE 4</title>
<title>TITLE 5</title>
ETC...
有超過 1200 個字串,只是為了讓您知道。我還有一個具有相同數量元素的 XML 檔案,每個元素都采用以下格式:
<game id="N">
<url>URL</url>
<img>IMG_URL</img>
<date>DATE</date>
<genre>GENRE</genre>
<publisher>PUBLISHER</publisher>
<developer>DEVELOPER</developer>
<platforms>PLATFORMS</platforms>
<vote>VOTE</vote>
<review>REVIEW_URL</review>
<info><![CDATA[INFORMATIONS]]></info>
</game>
我需要做的是為each和 before<title>之間的每個字串附加。可以使用 VB.NET 來完成(我不能用 C#/C 撰寫代碼),如果可以,有人可以幫我撰寫代碼嗎?或者也許有一個更簡單的正則運算式等方法......謝謝!</img><date>
uj5u.com熱心網友回復:
使用 XElement 和 LINQ。
Dim URI As String = "your path here"
Dim fileXE As XElement
'fileXE = XElement.Load(URI) ' - load XML, uncomment in production
'for testing = remove in production
Dim titls As New List(Of String)
titls.AddRange({"<title>TITLE 1</title>",
"<title>TITLE 2</title>",
"<title>TITLE 3</title>",
"<title>TITLE 4</title>",
"<title>TITLE 5</title>"})
'for testing = remove in production
fileXE = <games>
<game id="A">
<url>URL</url>
<img>IMG_URL</img>
<date>DATE</date>
<genre>GENRE</genre>
<publisher>PUBLISHER</publisher>
<developer>DEVELOPER</developer>
<platforms>PLATFORMS</platforms>
<vote>VOTE</vote>
<review>REVIEW_URL</review>
<info><![CDATA[INFORMATIONS]]></info>
</game>
<game id="B">
<url>URL</url>
<img>IMG_URL</img>
<date>DATE</date>
<genre>GENRE</genre>
<publisher>PUBLISHER</publisher>
<developer>DEVELOPER</developer>
<platforms>PLATFORMS</platforms>
<vote>VOTE</vote>
<review>REVIEW_URL</review>
<info><![CDATA[INFORMATIONS]]></info>
</game>
<game id="C">
<url>URL</url>
<img>IMG_URL</img>
<date>DATE</date>
<genre>GENRE</genre>
<publisher>PUBLISHER</publisher>
<developer>DEVELOPER</developer>
<platforms>PLATFORMS</platforms>
<vote>VOTE</vote>
<review>REVIEW_URL</review>
<info><![CDATA[INFORMATIONS]]></info>
</game>
</games>
'end for testing
'get all img nodes
Dim imgs As IEnumerable(Of XElement) = fileXE...<img>
For Each el As XElement In imgs 'add one string per img
Dim xe As XElement
Try
xe = XElement.Parse(titls(0)) 'get a title
titls.RemoveAt(0) 'remove it
el.AddAfterSelf(xe) 'add it
Catch ex As Exception
Stop
End Try
Next
fileXE.Save(URI) 'save file
Stop
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473632.html
上一篇:當它位于除錯檔案夾中時,如何覆寫VisualBasic中的文本檔案?
下一篇:VB.NET創建Sub的字典
