[英]replacing inner text of an invalid xml file
I want to replace inner text of the tag of an XML file. 我想替换XML文件标签的内部文本。 The XML file I have is not well formed so we cannot use LINQ-TO-XML.
我拥有的XML文件格式不正确,因此我们无法使用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 在XML文件中,我只想将Jan替换为01。注意:月份名称可以在其他文件中更改,
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.
因此,基本上可以在无效的XML文件中找到替换上述行的表达式。
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 但是在某些文件中我得到了错误,例如xlink是未声明的前缀,而在另一些文件中我得到了不同的错误
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 它会给你OP喜欢
<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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.