tags:

views:

34

answers:

2

Below is my code to transform XML using XSLT:

Private Sub Transform(ByVal XslUri As String, ByVal XmlString As String, ByVal OutputUri As String)
    ' Create the XslTransform object and load the style sheet
    Dim xslt As New XslCompiledTransform()
    xslt.Load(XslUri)

    ' Load the file to transform
    Dim input As XmlReader = XmlReader.Create(New StringReader(XmlString))

    ' Create the writer to append
    Dim fileWriter As New StreamWriter(OutputUri, True)
    Dim output As XmlWriter = XmlWriter.Create( _
        fileWriter, _
        xslt.OutputSettings)

    ' Transform the file 
    xslt.Transform(input, output)
    output.Close()
End Sub

It worked fine until I came across this piece of data in an XML input: 34 &I40 #251

It doesn't appear to be escaping the &I40 properly. What can I do differently to make sure all of my data is escaped properly? Thanks.

+2  A: 

If you can find the problematic content, take a look at System.Security.SecurityElement.Escape to get rid of it.

As for finding it, I guess you're going to have to walk the source tree node by node and fix up problems as you find them, but content like that will probably jam up your XmlReader on the way in, so if you have any control over the source files before you perform this translation it would be best to fix them when they are created if you possibly can. As has been commented, it's not valid XML if it contains those characters.

glenatron
As I posted above, my new suspicion of it being unescaped when I validate and remove nodes with errors was confirmed. I am now using System.Security.SecurityElement.Escape() on the data is it runs through and am not getting errors. Thank you!
heath
A: 

Your XML is invalid. You'll have to run it through some sort of tidy utility to ensure it is valid before attempting to apply XSLT. HTML Tidy would probably be good enough if you don't have anything overly complex in your XML such as CDATA sections or DTD subsets.

Josh Stodola