繁体   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