Solved
Replacing special XML characters in vb.net
Posted on 2011-09-07
I've got a website where a user needs to print details from a customer info screen onto an envelope to be mailed to said customer.
I have an xml template doc that is on the server and a function that pulls info from the customer form and makes strings (name and address strings) and places those into the template. When the button is clicked to generate it opens an instance of MS Word on their local machine and allows them to print the envelope with all the details entered for them.
The only issue now is when you have an address or company name with a "&" in it the function crashes out as the "&" is a special character in XML. What's the best way to compare the string and replace the "&"? I'll give the code example below:
Public Shared Function CreateEnvelopeForPrint(ByVal LicenceFormParam As LicenceForm, ByVal TemplateStringParam As String) As String
Dim Addr1 As String = ""
Dim Addr2 As String = ""
Dim Addr3 As String = ""
Dim Addr4 As String = ""
Dim Addr5 As String = ""
With LicenceFormParam
''Creating the strings to hold the customer's names and addresses
Dim PrimaryOwner As String = .PrimaryFirstName + " " + .PrimaryLastName
Dim SecondaryOwner As String = .SecondaryFirstName + " " + .SecondaryLastName
' Build line that contains place, province/state and postal code
Dim PlaceLineStr As String
If .ProvinceCode.Equals("99") Then
PlaceLineStr = .PlaceName + ", " + .CountryName
Else
PlaceLineStr = .PlaceName + " " + .ProvinceCode + " " + .PostalCode
End If
If (.EnterpriseName <> "") Then
Addr1 = .EnterpriseName
If (.PrimaryLastName <> "") Then
Addr2 = PrimaryOwner
If (.SecondaryLastName <> "") Then
Addr2 = Addr2 + " / " + SecondaryOwner
End If
Else
Addr2 = .AddressLine1
End If
Else
Addr1 = PrimaryOwner
If (.SecondaryLastName <> "") Then
Addr1 = Addr1 + " / " + SecondaryOwner
End If
Addr2 = .AddressLine1
End If
If Addr2 = .AddressLine1 Then
If .AddressLine2 <> "" Then
Addr3 = .AddressLine2
Addr4 = PlaceLineStr
Else
Addr3 = PlaceLineStr
End If
Else
Addr3 = .AddressLine1
If .AddressLine2 <> "" Then
Addr4 = .AddressLine2
Addr5 = PlaceLineStr
Else
Addr4 = PlaceLineStr
End If
End If
End With
'Replace lines $ADDRX$ place holders in template with the appropriate upper case line built above
TemplateStringParam = TemplateStringParam.Replace("$ADDR1$", Addr1.ToUpper)
TemplateStringParam = TemplateStringParam.Replace("$ADDR2$", Addr2.ToUpper)
TemplateStringParam = TemplateStringParam.Replace("$ADDR3$", Addr3.ToUpper)
TemplateStringParam = TemplateStringParam.Replace("$ADDR4$", Addr4.ToUpper)
TemplateStringParam = TemplateStringParam.Replace("$ADDR5$", Addr5.ToUpper)
Return TemplateStringParam
End Function