Solved

ASP.NET and XML Encoding

Posted on 2004-08-13
3
167 Views
Last Modified: 2010-04-07
I have a procedure that takes in a path to the file and a string containing xml data, and then writes it to the file to which the path is provided:

Sub WriteXMLToFile(ByVal filePath As String, ByVal xmltext As String)
        Dim xsr As XmlTextReader
        Dim xsw As XmlTextWriter
        Dim file As String

        Try
            xsr = New XmlTextReader(filePath)
            file = xsr.ReadInnerXml
            xsr.Close()
            file = xmltext
            xsw = New XmlTextWriter(filePath, Encoding.UTF8)
            xsw.WriteString(file)

        Catch ex As Exception
            Response.Write("The following error has occurred when writing to a file: " & ex.Message)
        Finally
            xsw.Close()
        End Try
    End Sub

The problem with the procedure is that it replaces < and > signs with &lt; and &gt;

How can I avoid that?
0
Comment
Question by:YZlat
  • 3
3 Comments
 
LVL 15

Accepted Solution

by:
Thogek earned 80 total points
ID: 11797051
Maybe because it thinks you're writing a XML node's value, when you're trying to write the XML node itself....

If your xmltest variable already contains known-to-be-valid XML text, then perhaps something simpler such as:

    Sub WriteXMLToFile(ByVal filePath As String, ByVal xmltext As String)
        Dim sw As StreamWriter

        Try
            StreamWriter = New StreamWriter(filePath, False, Encoding.UTF8)
            sw.Write(xmltext)
            sw.Close()
        Catch ex As Exception
            Response.Write("The following error has occurred when writing to a file: " & ex.Message)
        Finally
            xsw.Close()
        End Try
    End Sub
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11797069
Here's the XmlTextWriter.WriteString method (that you're currently using), for comparison: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemxmlxmltextwriterclasswritestringtopic.asp
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11797081
Or, if you'd rather stick with the XmlTextWriter, try switching from the WriteString method to using the WriteRaw Method.  http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemxmlxmltextwriterclasswriterawtopic1.asp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now