CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2007
    Posts
    30

    Exclamation C# writing XML through XmlTextWriter

    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

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: C# writing XML through XmlTextWriter

    What about adding changing it this way:
    Code:
    XW.WriteString(XR.ReadContentAsString(), False)
    At first, I would recommend to inspect return value of XR.ReadContentAsString() to see what exactly have you read.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured