سؤال

I have a page that's used for editing a particular item in an XML file. I already have it working, but the thing is, there are a couple of elements there that are 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>

I have this Sub that enables me to save the modifications:

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

Unfortunately, whenever I save an entry, it removes the CDATA entry, so the node looks like this:

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

It should look like this:

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

How do I retain the CDATA element when I save the modification?

TIA!

هل كانت مفيدة؟

المحلول

You can do that by reading content of particular element (which contains CData) as XmlCDataSection, then update it's InnerText property :

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

نصائح أخرى

Just wrap the text in a new CData node, like this:

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 now looks like:

<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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top