您如何使用数据清理类中的数据集合 [英] How do you data scrub a collection of data in a class
问题描述
我该如何清理数据集合中的数据?
我正在使用Windows应用程序的现有VB.NET代码,该代码使用StreamWriter和Serializer输出事务数据的XML文档.下面的代码.
How can I data scrub a collection of data?
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()
XML文档正在上传到另一个不接受"crlf"的应用程序.
"TransactionFile"是名为ProjectSchema.TransactionFile的类中的数据集合.它包含各种数据类型.
有5个函数可用于创建有助于创建名为TransactionFile的主事务文件的节点.
我需要在数据收集中找到CRLF字符,然后将CRLF字符替换为空格.
我可以在字段级别用以下字符替换非法字符:
The XML document is being uploaded to another application that does not accept "crlf".
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 collection of 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.
If I try:
TransactionFile = Regex.Replace(TransactionFile, "[^A-Za-z0-9\-/]", " ")
我收到从交易"类型转换为字符串"无效类型"消息.
I get "Conversion from type ''Transaction'' to type ''String'' is not valid" message.
推荐答案
您需要循环浏览每一项.
虽然它是一个集合,但该集合是一个对象,但是集合中的每个项目也是如此.
对于每个循环,一个简单的方法就可以解决问题.
You would need to loop through each item.
Altough it is a collection, the collection is an object, but so is each item in the collection.
A simple for each loop should do the trick.
我决定不着手攻击整个TransactionFile,而是专注于为TransactionFile做出贡献的每个节点.
例如,我决定解析自定义类中每个属性的值-Location
我能够通过反思来做到这一点:
Instead of attacking the entire TransactionFile, I decided to focus on each node that contributed to the TransactionFile.
For example, I decided to parse through the values of each property in the custom class - Location
I was able to accomplish this through reflection:
Public Function ScrubData(ByRef STransaction)
Dim sTable As New ProjectSchema.Location
Dim property1 As String
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
这篇关于您如何使用数据清理类中的数据集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!