Replacing file using regex

后端 未结 2 1554
北海茫月
北海茫月 2021-01-23 16:49

i tried to replace inner text of the month tag i.e month names should be replaced with their specified month number. i tried this,

 Dim strFile As String = File.         


        
2条回答
  •  情话喂你
    2021-01-23 17:20

    You could do it like this:

    Dim doc As New XmlDocument()
    Dim months As IDictionary(Of String, String) = New Dictionary(Of String, String)() From {{"January", "1"}, {"February", "2"}, {"March", "3"}, {"April", "4"}, {"May", "5"}, {"June", "6"}, {"July", "7"}, {"8", "August"}, {"September", "9"}, {"October", "10"}, {"November", "11"}, {"December", "12"}}
    Dim expr As New Regex([String].Join("|", months.Keys))
    Dim strFile As String = "May"
    
    doc.Load(TextBox1.Text & "\" & parentFolder & ".xml")
    
    For Each item As XmlNode In doc.GetElementsByTagName("month")
        item.Value = expr.Replace(item.Value, Function(m) months(m.Value))
    Next
    

提交回复
热议问题