如何通过 VB.NET 修改 XML 中的 CData
Posted
技术标签:
【中文标题】如何通过 VB.NET 修改 XML 中的 CData【英文标题】:How to modify CData in XML via VB.NET 【发布时间】:2014-02-12 08:29:41 【问题描述】:我有一个用于编辑 XML 文件中特定项目的页面。我已经让它工作了,但问题是,有几个元素是 CDATA:
<Item>
<Item_Number></Item_Number>
<Category>Vibration</Category>
<Language></Language>
<Description></Description>
<Long_Description><![CDATA[]]></Long_Description>
<BOM>
<![CDATA[]]>
</BOM>
<Recommended_Parts></Recommended_Parts>
<Picture></Picture>
</Item>
我有这个 Sub 可以让我保存修改:
Private Sub SaveData(ByVal fileName As String, ByVal strSelection As String)
lblData.Text = ""
Dim strContent As String = ""
Dim m_xmld As XmlDocument
'Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
m_xmld.Load(fileName)
m_node = m_xmld.SelectSingleNode(strSelection)
Dim idValue As String = txtID.Text
Dim catValue As String = txtCat.Text
Dim lngValue As String = txtLang.Text
Dim dscValue As String = txtDsc.Text
Dim ldcValue As String = txtLdc.Text
Dim bomValue As String = txtBom.Text
Dim recValue As String = txtRec.Text
Dim picValue As String = txtPic.Text
m_node.ChildNodes.Item(0).InnerText = idValue
m_node.ChildNodes.Item(1).InnerText = catValue
m_node.ChildNodes.Item(2).InnerText = lngValue
m_node.ChildNodes.Item(3).InnerText = dscValue
m_node.ChildNodes.Item(4).InnerText = ldcValue
m_node.ChildNodes.Item(5).InnerText = bomValue
m_node.ChildNodes.Item(6).InnerText = recValue
m_node.ChildNodes.Item(7).InnerText = picValue
m_xmld.Save(fileName)
MsgBox("Entry saved!")
End Sub
不幸的是,每当我保存条目时,它都会删除 CDATA 条目,因此节点如下所示:
<Item>
<Item_Number>test003</Item_Number>
<Category>Vibration</Category>
<Language>Japanese</Language>
<Description>description</Description>
<Long_Description>rsxgrdxgtxtxg</Long_Description>
<BOM>xwthgwtg
trxthtrh
trxeyrxjyetj
txrhueyjh</BOM>
<Recommended_Parts>xtrwth</Recommended_Parts>
<Picture>pic.jpg</Picture>
</Item>
应该是这样的:
<Item>
<Item_Number>test003</Item_Number>
<Category>Vibration</Category>
<Language>Japanese</Language>
<Description>description</Description>
<Long_Description><![CDATA[rsxgrdxgtxtxg]]></Long_Description>
<BOM><![CDATA[xwthgwtg
trxthtrh
trxeyrxjyetj
txrhueyjh]]></BOM>
<Recommended_Parts>xtrwth</Recommended_Parts>
<Picture>pic.jpg</Picture>
</Item>
保存修改时如何保留 CDATA 元素?
TIA!
【问题讨论】:
【参考方案1】:您可以通过将特定元素(包含 CData)的内容读取为 XmlCDataSection
来做到这一点,然后更新它的 InnerText
属性:
Dim CData As XmlCDataSection = m_node.ChildNodes.Item(5).ChildNodes(0)
CData.InnerText = txtBom.Text
【讨论】:
我收到此错误:无法将“System.Xml.XmlText”类型的对象转换为“System.Xml.XmlCDataSection”类型。 :( 好的,找到了解决方案 :) 愚蠢的我,我正在编辑的那个还没有在 CDATA 上。现在一切正常 :) 谢谢! @Poch 你可以分享的解决方案是什么,我有你评论的同样的错误【参考方案2】:只需将文本包装在一个新的CData
节点中,如下所示:
Dim xml = <Item>
<Item_Number></Item_Number>
<Category>Vibration</Category>
<Language></Language>
<Description></Description>
<Long_Description><![CDATA[]]></Long_Description>
<BOM>
<![CDATA[]]>
</BOM>
<Recommended_Parts></Recommended_Parts>
<Picture></Picture>
</Item>
With xml
.<Item_Number>(0).Value = "test003"
.<BOM>(0).ReplaceNodes(<![CDATA[xwthgwtgtrxthtrhtrxeyrxjyetjtxrhueyjh]]>)
.<Picture>(0).Value = "pic.jpg"
End With
xml
现在看起来像:
<Item>
<Item_Number>test003</Item_Number>
<Category>Vibration</Category>
<Language></Language>
<Description></Description>
<Long_Description><![CDATA[]]></Long_Description>
<BOM><![CDATA[xwthgwtgtrxthtrhtrxeyrxjyetjtxrhueyjh]]></BOM>
<Recommended_Parts></Recommended_Parts>
<Picture>pic.jpg</Picture>
</Item>
【讨论】:
以上是关于如何通过 VB.NET 修改 XML 中的 CData的主要内容,如果未能解决你的问题,请参考以下文章