簡體   English   中英

替換xml文件中的內部文本

[英]replacing inner text in a xml file

我發現了這個表情

^[<conf-start a-z0-9- =""/><month></month>]+

匹配源字符串

<conf-start iso-8601-date="2011-03-04"><day>04</day><month>March</month><year>2011</year></conf-start>

但我無法將3月份替換為03月份,例如3月份應替換為03

我嘗試了以下代碼,

    Dim mydir As String = TextBox1.Text
    Dim filePath As String = TextBox1.Text
    Dim directory1 As String = Path.GetDirectoryName(filePath)
    Dim split As String() = filePath.Split("\")
    Dim parentFolder As String = split(split.Length - 2)

    Dim outputLines As New List(Of String)()
    Dim stringToMatch As String = "^[<conf-start a-z0-9- =""/><month>March</month>]+"
    Dim replacementString As String = "^[<conf-start a-z0-9- =""/><month>03</month>]+"

    For Each line As String In System.IO.File.ReadAllLines(TextBox1.Text & "\" & parentFolder & ".xml")
        Dim matchFound As Boolean
        matchFound = line.Contains(stringToMatch)

        If matchFound Then
            ' Replace line with string
            outputLines.Add(replacementString)
        Else
            outputLines.Add(line)
        End If
    Next
    System.IO.File.WriteAllLines(TextBox1.Text & "\" & parentFolder & ".xml", outputLines.ToArray(), Encoding.UTF8)

但我無法在xml文件中找到更改。 所以有人請幫我 仍然堅持這個問題。

您可以改用LINQ to XMLXML文字

例:

' Load the file '
Dim yourXml = XDocument.Load("your_path_to_the_file.xml").Root

' Change each monthname to the number of the month
For Each elem in yourXml ...<conf-start>.<month>
    elem.value = DateTime.ParseExact(elem.value, "MMMM", System.Globalization.CultureInfo.InvariantCulture).Month
Next

' save file
yourXml.Save("your_path_to_the_file.xml")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM