2

I try to write a little program to export data from an SQL database to XML (and later on to import the same data in another database). For that I get the schema of the tables in question, read all the data from the tables and putting it into an XML structure to save when done with all the tables.

I use the XDocument as the XML structure.

Dim result As XDocument
Dim element As XElement
Dim item As XElement

result = New XDocument(New XDeclaration("1.0", "utf-8", "yes"))
element = New XElement("table", New XAttribute("name", <variable holding the table name>))
result.Add(element)

item = New XElement("item")
element.Add(item)

item.Add(New XAttribute(<variable holding the column name>, <variable holding the column data>))
...

So far, so good.

Using the XDocument even seems to take care of masking special characters (so I thought).

But now I get an error because the value of one of the string columns I'd like to export contains an invalid character (ChrW(1)). The error doesn't happen when adding the XAttribute with the invalid data, it happens when trying to save the XML content to a file or when getting the XML content via ToString. After hours of inspecting the data I found out which table and which data item contains the invalid characters, but how do I have to mask them to be able to export them to XML? Is there any built in method or do I have to write it myself?

Nostromo
  • 1,177
  • 10
  • 28
  • https://stackoverflow.com/questions/6784337/how-to-include-cdata-using-linq-to-xml – Lex Li Sep 16 '18 at 14:33
  • Putting "<>" infront of the value didn't help and putting the value inside a `XCData` object brings another error (an XObject can't be used as value). – Nostromo Sep 18 '18 at 05:10

0 Answers0