使用VB.net基于特定属性值将Delimiter文件转换为XML。 [英] Convert Delimiter file to XML, based on the particular attribute value using VB.net.
问题描述
下面是我的输入文件数据,它是 pipe 分隔。
T1234 | ABC LTD | 100 | R1111
9876 | 2017-10-27 || R1111
9875 | 2017-10-31 || R1111
9874 | 2017-10-27 || R1111
9873 | 2017-10-27 || R1111
T4567 | DEF | 2858.06 | R2222
9872 | 2017-10-27 || R2222
T7891 | FGH | 1000 | R3333
9873 | 2017-10-27 || R3333
如果我们观察上述文件数据我们可以在第4位找到共同值。
第一行是标题记录数据,第二行是详细记录数据。
例如:' R1111 '是前五行的共同点,它是一套,与' R2222 '依此类推,..
例外情况:
<?xml version =" 1.0"编码= QUOT; UTF-8英寸;?>
<付款>
< P A01 =" T1234" A02 ="ABC LTD" A03 = QUOT; 100" A04 = QUOT; R1111">
< R A01 =" 9876" A02 = QUOT; 2017年10月27日" A03 = QUOT;" A04 = QUOT; R1111" />
< R A01 =" 9875" A02 = QUOT; 2017年10月31日" A03 = QUOT;" A04 = QUOT; R1111" />
< R A01 =" 9874" A02 = QUOT; 2017年10月27日" A03 = QUOT;" A04 = QUOT; R1111" />
< R A01 =" 9873" A02 = QUOT; 2017年10月27日" A03 = QUOT;" A04 = QUOT; R1111" />
< / P>
< P A01 =" T4567" A02 = QUOT; DEF" A03 = QUOT; 2858.06" A04 = QUOT; R2222">
< R A01 =" 9872" A02 = QUOT; 2017年10月27日" A03 = QUOT;" A04 = QUOT; R2222" />
< / P>
< P A01 =" T7891" A02 = QUOT; FGH" A03 = QUOT; 1000" A04 = QUOT; R3333">
< R A01 =" 9873" A02 = QUOT; 2017年10月27日" A03 = QUOT;" A04 = QUOT; R3333" />
< / P>
< /付款>
您能否请求帮助。
提前致谢!
你好,
我没有时间给出一个完整的例子,但下面将文本文件中的数据读入列表然后对数据进行分组。剩下的就是创建可以使用XML文字和嵌入式表达式完成的xml。如果你没有获得
的xml部分,我今天晚些时候可以帮助它。
文本文件
T1234 | ABC LTD | 100 | R1111
9876 | 2017-10-27 || R1111
9875 | 2017-10-31 || R1111
9874 | 2017-10-27 | | R1111
9873 | 2017-10-27 || R1111
T4567 | DEF | 2858.06 | R2222
9872 | 2017-10-27 || R2222
T7891 | FGH | 1000 | R3333
9873 | 2017-10-27 || R3333持有数据的具体类
Public Class DataItem
Public Property A1 As String
Public Property A2 As String
Public Property A3 As String
Public Property A4 As String
End Class
从文本文件中读取数据的类
Imports System.IO
公共类操作
公共函数Read()As List(Of DataItem)
Dim itemList As New List(Of DataItem)
Dim fileName As String = Path.Combine(AppDomain .CurrentDomain.BaseDire ctory," Data.txt")
使用MyReader作为新FileIO.TextFieldParser(fileName)
MyReader.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String(){" |"}
Dim currentRow As String()
而不是MyReader.EndOfData
尝试
currentRow = MyReader.ReadFields ()
itemList.Add(New DataItem With
{
.A1 = currentRow(0),
.A2 = currentRow(1),
.A3 = currentRow (2),
.A4 = currentRow(3)
})
Catch ex As FileIO.MalformedLineException
'
'你需要决定如何更好地处理这个问题比这个
'
Console.Write(ex.Message)
结束尝试
结束
结束使用
返回itemList
结束函数
结束类
在表格中获取数据
Public Class Form1
Private Sub Button1_Click(sender As Object,e As EventArgs)处理Button1.Click
Dim ops As New Operations
Dim results = ops.Read
Dim Groups = results.GroupBy(Function(x)x .A4)
对于组中的每个项目
Console.WriteLine(item.Key)
For each subItem in item
Console.WriteLine(
" {subItem.A1},{subItem.A2},{subItem.A3},{subItem.A4}")
下一个
下一个
结束次级
结束等级
从上面的代码中导出Visual Studio中的输出窗口
R1111
T1234, ABC LTD,100,R1111
9876,2017-10-27 ,, R1111
9875,2017-10-31 ,, R1111
9874,2017-10-27 ,, R1111
9873,2017-10-27 ,, R1111
R2222
T4567,DEF,2858.06,R2222
9872,2017-10-27 ,, R2222
R3333
T7891,FGH,1000,R3333
9873,2017-10-27 ,, R3333
Below is the my input file data and it is pipe delimited.
T1234|ABC LTD|100|R1111
9876|2017-10-27||R1111
9875|2017-10-31||R1111
9874|2017-10-27||R1111
9873|2017-10-27||R1111
T4567|DEF|2858.06|R2222
9872|2017-10-27||R2222
T7891|FGH|1000|R3333
9873|2017-10-27||R3333
If we observer the above file data we can find the common value at 4th position.
First line is the header record data and from second line detail records data.
Ex: 'R1111' is the common for first five lines and it is one set, same as 'R2222' and so on,..
Excepted out put:
<?xml version="1.0" encoding="utf-8"?> <Payments> <P A01="T1234" A02="ABC LTD" A03="100" A04="R1111"> <R A01="9876" A02="2017-10-27" A03="" A04="R1111"/> <R A01="9875" A02="2017-10-31" A03="" A04="R1111"/> <R A01="9874" A02="2017-10-27" A03="" A04="R1111"/> <R A01="9873" A02="2017-10-27" A03="" A04="R1111"/> </P> <P A01="T4567" A02="DEF" A03="2858.06" A04="R2222"> <R A01="9872" A02="2017-10-27" A03="" A04="R2222"/> </P> <P A01="T7891" A02="FGH" A03="1000" A04="R3333"> <R A01="9873" A02="2017-10-27" A03="" A04="R3333"/> </P> </Payments>
Could you please help on this requirement.
Thanks in advance!
Hello,
I don't have time to give a complete example but the following reads data from a text file into a list then groups the data. What is left is to create the xml which can be done using XML literals and embedded expressions. If you don't get the xml part of nobody assist with it I can later today.
Text file
T1234|ABC LTD|100|R1111 9876|2017-10-27||R1111 9875|2017-10-31||R1111 9874|2017-10-27||R1111 9873|2017-10-27||R1111 T4567|DEF|2858.06|R2222 9872|2017-10-27||R2222 T7891|FGH|1000|R3333 9873|2017-10-27||R3333Concrete class for holding data
Public Class DataItem Public Property A1 As String Public Property A2 As String Public Property A3 As String Public Property A4 As String End ClassClass to read data from text file
Imports System.IO Public Class Operations Public Function Read() As List(Of DataItem) Dim itemList As New List(Of DataItem) Dim fileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data.txt") Using MyReader As New FileIO.TextFieldParser(fileName) MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited MyReader.Delimiters = New String() {"|"} Dim currentRow As String() While Not MyReader.EndOfData Try currentRow = MyReader.ReadFields() itemList.Add(New DataItem With { .A1 = currentRow(0), .A2 = currentRow(1), .A3 = currentRow(2), .A4 = currentRow(3) }) Catch ex As FileIO.MalformedLineException ' ' You need to decide how to handle this better than this ' Console.Write(ex.Message) End Try End While End Using Return itemList End Function End ClassIn a form get the data
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim ops As New Operations Dim results = ops.Read Dim Groups = results.GroupBy(Function(x) x.A4) For Each item In Groups Console.WriteLine(item.Key) For Each subItem In item Console.WriteLine(
" {subItem.A1},{subItem.A2},{subItem.A3},{subItem.A4}") Next Next End Sub End ClassResults in the Output window in Visual Studio from above code
R1111 T1234,ABC LTD,100,R1111 9876,2017-10-27,,R1111 9875,2017-10-31,,R1111 9874,2017-10-27,,R1111 9873,2017-10-27,,R1111 R2222 T4567,DEF,2858.06,R2222 9872,2017-10-27,,R2222 R3333 T7891,FGH,1000,R3333 9873,2017-10-27,,R3333
这篇关于使用VB.net基于特定属性值将Delimiter文件转换为XML。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!