Solved

ASP.NET and XML Encoding

Posted on 2004-08-13
3
177 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to go to Web page 2 98
Please explain: Aspect Oriented Programming 2 84
SQL best practice question 3 36
Syntax error 9 45
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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

14 Experts available now in Live!

Get 1:1 Help Now