Solved

ASP.NET and XML Encoding

Posted on 2004-08-13
3
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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