I am working with existing VB.NET code for a Windows Application that uses StreamWriter and Serializer to output an XML document of transaction data. Code below.

Private TransactionFile As ProjectSchema.TransactionFile

Dim Serializer As New Xml.Serialization.XmlSerializer(GetType(ProjectSchema.TransactionFile))
Dim Writer As TextWriter
Dim FilePath As String

Writer = New StreamWriter(FilePath)

Serializer.Serialize(Writer, TransactionFile)
Writer.Close()

The XML document is being uploaded to another application that does not accept "crlf".

My question is:
How can I scrub the TransactionFile data before, during or after the XML document is created so that data is acceptable for the other application?

If I try:

TransactionFile = Regex.Replace(TransactionFile, "[^A-Za-z0-9\-/]", " ")

I get "Conversion from type 'Transaction' to type 'String' is not valid" message.

Thanks in advance for any and all input.

Have you tried TransactionFile.ToString ?
This might also be of some help.

Have you tried TransactionFile.ToString ?
This might also be of some help.

Thanks for the reply,

Perhaps I have not given an adequate description of my inquiry. The "TransactionFile" is a collection of data in a Class named ProjectSchema.TransactionFile. It contains various data types.

There are 5 functions to create nodes that contribute to the creation of a Master Transaction file named TransactionFile

I need to find CRLF characters in the data and replace the CRLF characters with a space.

I am able to replace illegal characters at the field level with

.Name = Regex.Replace((Mid(CustomerName.Name, 1, 30)), "[^A-Za-z0-9\-/]", " ")

But I need to scrub the entire collection of data.

Have you tried a For/Next loop, to loop through all fields in the collection and replace data in each field if needed?

Have you tried a For/Next loop, to loop through all fields in the collection and replace data in each field if needed?

Thanks for the reply,

My problem is, according to my limited knowledge, "Replace" only works on String objects.
How do you For/Next through a collection of Objects and target only the String objects?

Cannot state what is unknown.
If possible, post your collections code and how you get a certain object/value from it.
Also, if the collection loads from a file, post some of the file content as well.
.Should be easy to try and provide a solution then, hopefully.

Pardon my failure to adequately describe my inquiry.

This is an example of the code I am dealing that creates a transaction. The transaction is converted to XML and then exported. (I have changed the names in hopes of retaining confidentiality hopefully without breaking it)

Function BuildMasterTransaction(ByVal DSTransaction As DataSetTransactions.TableTransactionsRow, ByVal SeqNumber As Integer, _
        ByVal b_GetCustomer As GetCustomer, ByVal c_GetProduct As GetProduct, ByVal d_GetAccount As GetAccount) _
     As ProjectSchema.Transaction

        Dim z_Transaction As New ProjectSchema.Transaction
        Dim x_Transactions As New DataSetTransactions
        Dim Type As String
        
        Try

            'Create MasterTransaction node
            psTransaction.MasterTransaction = GetMasterTransaction(a_Transaction)
            Type = z_Transaction.MasterTransaction.TransactionType
            z_Transaction.MasterTransaction.TransactionSeqNumber = SeqNumber


            If Type <> "Cancel" Then

                'Create Customer node
                b_GetCustomer.Invoke(DSTransaction.ID, z_Transaction, DSTransaction, SeqNumber)

                'Create Product nodes
                c_GetProduct.Invoke(DSTransaction.ID, z_Transaction, DSTransaction, SeqNumber)
                
                'Create Account node
                d_GetAccount.Invoke(DSTransaction.ID, z_Transaction, DSTransaction, SeqNumber)
             
            End If
			
			Catch ex As Exception
			Error = String.Concat(Error, vbCrLf, ex.Message, vbCrLf, ex.StackTrace)
            Me.m_oErrors.Add(Error)
            z_Transaction = Nothing
			
		End Try

        Return z_Transaction

    End Function

z_Transaction is the TransactionFile I mentioned in my original post that is used to create the XML.

If I try :

Dim TransactionData as datarow
For Each TransactionData In z_Transaction
                Regex.Replace(TransactionData, "vbCrLf", " ")
        Next
End If

I get a "datarow cannot be converted to a String" error on the "Replace" line of code.

Rather than validate each field,I am trying to validate z_Transaction.

Perhaps I am going about it the wrong way?

See if this helps.

Dim dt As New DataTable
        With dt '// FOR TESTING. 2 Columns with 2 Rows.
            With .Columns : .Add("1") : .Add("2") : End With
            With .Rows : .Add("a" & vbCrLf & "1", "b" & vbCrLf & "1") : .Add("a2" & vbCrLf & "1", "b2" & vbCrLf & "1") : End With
        End With
        '// loop thru DataTable.
        For Each row As DataRow In dt.Rows '// loop thru Rows.
            For Each column As DataColumn In dt.Columns '// loop thru each Column in Row.
                MsgBox("Before .Replace:" & vbNewLine & row(column).ToString) '// FOR TESTING.
                row(column) = row(column).ToString.Replace(vbCrLf, " ") '// replace vbCrLf with " " in each column, for each row.
                MsgBox("After .Replace:" & vbNewLine & row(column).ToString) '// FOR TESTING.
            Next
        Next

I can use:

Public Function ScrubData(ByRef STransaction)
        
        Dim sTable As New ProjectSchema.Transaction()
        Dim row1 As String
        sTable = STransaction
        row1 = sTable.LastName()
                  If row1 <> " " Or row1 <> "" Then
                       Regex.Replace((row1), "[^A-Za-z0-9\-/]", " ")
                  End If
  End Function

to access individual fields ie sTransaction.LastName or sTransaction.FirstName, no problem.

What I want is to access all the fields in sTable. If try something like:

Public Function ScrubData(ByRef STransaction)
        
        Dim sTable As New PointSchema.TransactionMaster()
        Dim row1 As String
        sTable = STransaction
        row1 = sTable.I dont know ()
        'row1 = sTable.InsuredLastName()
        For Each row1 In sTable
            If row1 <> " " Or row1 <> "" Then
                Regex.Replace((row1), "[^A-Za-z0-9\-/]", " ")
            End If
        Next
End Function

I cant figure out how to get row1 to equal a variable for each field in sTable.

For some reason I can see Replies to this post on my IPhone, but they dont appear when I access from my laptop or Workstation at work.

To Respond to mail.sandhya2:
Question : Why we have to create the transaction file?

Its complicated and not part of my resposibilities to redesign the system. A transaction file is created and converted to an XML file for export to another system and application of a different langauge and technology.

Dim dt As New DataTable
        With dt '// FOR TESTING. 2 Columns with 2 Rows.
            With .Columns : .Add("1") : .Add("2") : End With
            With .Rows : .Add("a" & vbCrLf & "1", "b" & vbCrLf & "1") : .Add("a2" & vbCrLf & "1", "b2" & vbCrLf & "1") : End With
        End With
        '// loop thru DataTable.
        For Each row As DataRow In dt.Rows '// loop thru Rows.
            For Each column As DataColumn In dt.Columns '// loop thru each Column in Row.
                MsgBox("Before .Replace:" & vbNewLine & row(column).ToString) '// FOR TESTING.
                row(column) = row(column).ToString.Replace(vbCrLf, " ") '// replace vbCrLf with " " in each column, for each row.
                MsgBox("After .Replace:" & vbNewLine & row(column).ToString) '// FOR TESTING.
            Next
        Next

When I try this, and replace the dt with the transaction I am working with I get an "No default member found for type "Transaction"" message.

Wish I could be of further help, but I am completely confused on your issue.
.Good luck.

My main problem has been lack of knowledge. And Not knowing how to ask the right questions.

Now that I know that I am dealing with properties and values of a Custom Class, I was able to find a solution to accomplish what I need through reflection:

Public Function ScrubData(ByRef STransaction)
    Dim sTable As New ProjectSchema.Location    
    Dim property1 As Object
    Dim value1 As Object

    sTable = STransaction

    For Each p As System.Reflection.PropertyInfo In sTable.GetType().GetProperties()
        If p.CanRead Then
            property1 = p.Name 'for testing to identify Property Name
                value1 = p.GetValue(sTable, Nothing)
                If value1 <> " " Then
                    Regex.Replace((value1), "[^A-Za-z0-9\-/]", " ")
                End If        
        End If
    Next

End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.