Click to See Complete Forum and Search --> : C# writing XML through XmlTextWriter


mansoor_aziz
December 4th, 2008, 04:41 AM
Hi,

I am reading XML from database using

ExecuteXMLReader()

and writing it to a file using

XmlTextWriter(filepath,encoding)

but the problem is that I am getting invalid character error ...

"hexadecimal value 0x03, is an invalid character. Line 3518, position 260."

is there any way to remove invalid characters from XML, I am creating XML in database using "for XML Explicit " and also using <![CDATA[]]> tags from all data.

here is my code



XmlReader XR ;
XmlTextWriter XW ;

XML = ""
cmd = new SqlCommand()
cmd.Connection = sqlConn

cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "sp_name"
cmd.Parameters.AddWithValue("@p1", p1)
cmd.Parameters.AddWithValue("@p2", p2)
cmd.CommandTimeout = 0


XR = cmd.ExecuteXmlReader()


XW = New XmlTextWriter(rootPath & fileName, System.Text.Encoding.UTF8)
XW.Formatting = Formatting.Indented
XW.WriteNode(XR, False)



Thanks and please help me out.

Best Regards

Mansoor Aziz

boudino
December 4th, 2008, 07:14 AM
What about adding changing it this way:

XW.WriteString(XR.ReadContentAsString(), False)


At first, I would recommend to inspect return value of XR.ReadContentAsString() to see what exactly have you read.