如何通过VB.NET修改XML中的CData

时间:2014-02-12 08:29:42

标签: .net xml vb.net save edit

我有一个页面用于编辑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!

2 个答案:

答案 0 :(得分:1)

您可以通过将特定元素(包含CData)的内容读取为XmlCDataSection来执行此操作,然后更新它的InnerText属性:

Dim CData As XmlCDataSection = m_node.ChildNodes.Item(5).ChildNodes(0)
CData.InnerText = txtBom.Text

答案 1 :(得分:0)

将文本包装在新的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>