I have an Asp.Net application that allows the user to insert in to an XML document. Before the insertion is made, I must first ensure that the 'primary key' is unique. To do this, I have a asp:CustomValidator that calls a function called ValidateUniqueCategory. (See attached code)
In this code, I get the value of the relevant textbox (sender.ControlToValidate)
This works well if there are no special characters present, however if the user enters special charaters (', ", <, >, &) in to SenderControl.Text it fails. How can I handle this?
Using a CDATA section? Please explain if this will work
Declaring GalleryXPath as something other than String? There are many other types that sound useful but I haven't been able to get them to work. Types such as: XmlText, XmlCDataSection etc.
Thank you in advance
Protected Sub ValidateUniqueCategory(ByVal sender As Object, ByVal Args As ServerValidateEventArgs)
Dim GalleryXml As New XmlDocument()
Dim GalleryXmlDirectory As String = "/Flash/Gallery/"
Dim GalleryXmlFilename As String = "Gallery.xml"
Dim GalleryXmlPath As String = Server.MapPath(GalleryXmlDirectory + GalleryXmlFilename)
Dim Gallery As XmlNode = GalleryXml.DocumentElement
Dim GalleryXPath As String = ""
Dim SenderControl As TextBox
SenderControl = CType(Master.FindControl("MasterMainContent").FindControl(sender.ControlToValidate), TextBox)
If Not SenderControl Is Nothing Then
GalleryXPath = "/Gallery/Category[@Name='" & SenderControl.Text & "']"
Dim GalleryNode As XmlNode = Gallery.SelectSingleNode(GalleryXPath)
If Not GalleryNode Is Nothing Then
Args.IsValid = False