简体   繁体   中英

replacing inner text of an invalid xml file

I want to replace inner text of the tag of an XML file. The XML file I have is not well formed so we cannot use LINQ-TO-XML. Is there another way to accomplish this?

<conf-start ISO-8601-date="2011-05-31"><day>31</day><month>Jan</month><year>2011</year></conf-start>

Within the XML file I just want to replace Jan to 01. Note : The month name can change in different files and also the

ISO-8601-date="2011-05-31"

Can change. So basically is it possible to find the expression to replace the above line in an invalid XML file.

i tried this,

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 yourXml = XDocument.Load(TextBox1.Text & "\" & parentFolder & ".xml").Root

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

    ' save file
    yourXml.Save(TextBox1.Text & "\" & parentFolder & ".xml")

but in some files i get errors such as xlink is an undeclared prefix and in some other files i get different errors

Try This

Dim y = "<conf-start iso-8601-date=""2011-05-31""><day>31</day><month>Jan</month><year>2011</year></conf-start>"

   Dim Match = Regex.Match(y, "<month>([^>]*)<\/month>").Groups(1).ToString
   Regex.Replace(y, Match, DateTime.ParseExact(Match, "MMM", CultureInfo.CurrentCulture).Month.ToString)

It will give you OP Like

<conf-start iso-8601-date="2011-05-31"><day>31</day><month>01</month><year>2011</year></conf-start>

try this code :

    Dim ds As New DataSet

    ds.ReadXml("yourXmlFile.xml")
    If Not ds Is Nothing Then
        ds.Tables("conf-start").Rows(0)("month") = DateTime.ParseExact(ds.Tables(0).Rows(0)("month"), "MMM", CultureInfo.CurrentCulture).Month.ToString
        ds.WriteXml("yourXmlFile.xml")
    End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM