我有一個顯示樹結構的文本文件。空格數表示給定成員的級別。例如下面,用戶組的成員或亞組誰可以擁有成員等兒子:從.NET中的文本文件構建XML樹
MainGroup
Member1
Member2
Group1
Member11
Member12
Group12
Member21
Member22
Member3
對不起大家,我 第一次和第一個問題在這裏所以是搞清楚整個事情的格式。
這是我到目前爲止已經試過:(這是沒有必要的,但我需要的數據表,以顯示我的數據)
- 我讀的文本文件導入數據表。
通過每行(有一列),我創建一個節點。我找到了空格的數量。如果它爲零,我將屬性添加到此節點並將其添加到doc。如果它有空格,我循環並繼續向此節點添加子節點。這是事情不適合我的地方。
Sub ExportToEXML Dim datarow As DataRow Dim fileName As String = "" Dim level As Integer = 0 Dim counter As Integer = 0 Dim doc As XmlDocument = New XmlDocument Dim docNode As XmlNode = doc.CreateXmlDeclaration("1.0", "UTF-8", Nothing) doc.AppendChild(docNode) Dim ComponentsNode As XmlNode = doc.CreateElement("Components") doc.AppendChild(ComponentsNode) Dim firstrow As DataRow For i As Integer = 0 To dt.Rows.Count - 1 firstrow = dt.Rows.Item(i) fileName = firstrow(0) level = CountSpacesBeforeFirstChar(fileName) Dim partNode As XmlNode = doc.CreateElement("Component") Dim att As XmlAttribute = doc.CreateAttribute("Name") att.Value = fileName partNode.Attributes.Append(att) GetChildNodes(partNode, i, doc, 0, level, dt) ComponentsNode.AppendChild(partNode) Next doc.Save("D:\TestXML.xml") End Sub Private Sub GetChildNodes(ByRef xNode As XmlNode, ByRef rowInd As Integer, ByRef xDoc As XmlDocument, level As Integer, table As DataTable) Dim lev As Integer Dim fileName As String Dim dr As DataRow For i As Integer = rowInd + 1 To table.Rows.Count - 1 dr = table.Rows.Item(i) fileName = dr(0) lev = CountSpacesBeforeFirstChar(fileName) If lev = 0 Then 'has no children Exit Sub End If If lev > level Then Dim partNode As XmlNode = xDoc.CreateElement("Component") Dim att As XmlAttribute = xDoc.CreateAttribute("Name") att.Value = fileName partNode.Attributes.Append(att) xNode.AppendChild(partNode) GetChildNodes(xNode, i, xDoc, lev, table) End If Next End Sub
您嘗試了什麼?你在哪裏遇到問題?發佈您的代碼。 – qqbenq
你能解釋一下「空格數表示給定成員的級別」是什麼意思嗎?它是一個固定寬度的文本文件嗎? – wooters