locked
Adding CDATA to output of writexml from database RRS feed

  • Question

  • User1444011213 posted

    Hi,

    I need to add CDATA to my xml that is created from database so all values nodes  are inside <![CDATA[ some valee ]]>

    Here is my current code

    Dim connectionString As String = WebConfigurationManager.ConnectionStrings("dbTest").ConnectionString
    Dim selectSQL As String = ""
    selectSQL &= " SELECT *"
    selectSQL &= " FROM tblXMLtest"
    Dim con As New SqlConnection(connectionString)
    Dim cmd As New SqlCommand(selectSQL, con)
    Dim adapter As SqlDataAdapter
    Dim ds = New DataSet("items")

    Try
      con.Open()
      adapter = New SqlDataAdapter(selectSQL, connectionString)
      adapter.Fill(ds)
      ds.Tables(0).TableName = "item"
      ds.WriteXml(Path.Combine(Request.PhysicalApplicationPath, "testxml.xml"), Data.XmlWriteMode.IgnoreSchema)

    Catch Err As Exception
    l  blError.Text = "Error reading list of names. subCreateXML: "
      lblError.Text &= Err.Message
    Finally
      con.Close()
    End Try

    Please give me some idea ;) Thx

    Wednesday, August 3, 2016 3:14 AM

Answers

  • User36583972 posted

    Hi westgatezagreb,

    need to add CDATA to my xml that is created from database so all values nodes  are inside <![CDATA[ some valee ]]>

    I suggest you can do like the below.

    1: When you get data from your database. You can convert your datatable to a temptable, which convert values inside <![CDATA[ some valee ]]> format.

            DataTable dttableNew = new DataTable("TestTabelxml");
                for(int i=0;i < tblDatas.Columns.Count; i++)
                {
                    DataColumn cloum = dttableNew.Columns.Add(tblDatas.Columns[i].ColumnName, Type.GetType("System.String"));
                }
    
                for (int i = 0; i < tblDatas.Rows.Count; i++)
                {
                    DataRow nrow = dttableNew.NewRow();
                    for (int y = 0; y < tblDatas.Columns.Count; y++)
                    {
                        nrow[y] = "<![CDATA[ " + tblDatas.Rows[i][y].ToString() + " ]]>";
                    }
                    dttableNew.Rows.Add(nrow);
                }
    

    2: Then, convert datatable to xml file.

    Please note that your custom xml format should require to meet XML specifications.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 4, 2016 8:20 AM