首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

xml资料的读取 并在Treeview控件上显示

2011-12-19 
xml文件的读取 并在Treeview控件上显示?xmlversion 1.0 standalone yes ?WAWAlistgroup 好

xml文件的读取 并在Treeview控件上显示
<?xml   version= "1.0 "   standalone= "yes "?>
<WAWA>
    <list>
        <group> 好友 </group>
        <friend> A001 </friend>
    </list>
  <list>
        <group> 好友 </group>
        <friend> A002 </friend>
    </list>
    <list>
        <group> 陌生人 </group>
        <friend> B001 </friend>
    </list>
<list>
        <group> 陌生人 </group>
        <friend> B002 </friend>
    </list>
    <list>
        <group> 黑名单 </group>
        <friend> C001 </friend>
    </list>
<list>
        <group> 黑名单 </group>
        <friend> C002 </friend>
    </list>
<list>
        <group> 朋友 </group>
        <friend> D001 </friend>
    </list>
    <list>
        <group> 朋友 </group>
        <friend> D002 </friend>
    </list>
</WAWA>

这是一个XML文件的内容   我想读取XML上的数据并在treeview控件上面显示下面的效果
由于我水平有限,希望大家帮帮忙。


|————好友
|                     |——A001
|                     |——A002
|
|————陌生人
|                     |——B001
|                     |——B002
|
|————黑名单
|                     |——C001
|                     |——C002
|
|————朋友
|                     |——D001
|                     |——D002
|




[解决办法]
容易
需要知识
xml解析
dataset.readxml
xmldocument.read
xmlreader

树节点添加
treeview1.nodes.add();
[解决办法]
我发表的资源,有源码,可以运行的
:http://download.csdn.net/source/228583#aa
[解决办法]
1.先读取出xml文件存放到dataTable中
StreamReader str=new StreamReader( "~/tree.xml ");
XmlDataDocument document=new XmlDataDocument();
document.DataSet.ReadXml(str);
DataTable dt=document.DataSet.Tables[0];
2.使用递归,读取出第一个节点

[解决办法]
xml操作完整类

Public Class Xml_Cls

Private XmlDoc As XmlDocument
Private XmlFile As String

Public ReadOnly Property XmlFileName() As String
Get
Return XmlFile
End Get


End Property

Public ReadOnly Property XmlText() As String
Get
Return XmlDoc.InnerXml
End Get
End Property

Sub New(ByVal FileName As String, Optional ByVal CreateNew As Boolean = True, Optional ByVal Root As String = "XML ", Optional ByRef IsOK As Boolean = False)
IsOK = False
XmlFile = " "
Dim reader As System.Xml.XmlReader = Nothing
Try
reader = New System.Xml.XmlTextReader(FileName)
reader.Read()
Catch ex As Exception
If reader IsNot Nothing Then reader.Close()
Debug.Print( "New - " & ex.Message)
If Not Create(FileName, Root) Then Return
Finally
If reader IsNot Nothing Then reader.Close()
End Try

IsOK = True
XmlFile = FileName
XmlDoc = New XmlDocument
XmlDoc.Load(XmlFile)
End Sub

Public Function Create(ByVal FileName As String, Optional ByVal Root As String = "XML ") As Boolean
Dim NewXML As XmlTextWriter = Nothing
Try
NewXML = New XmlTextWriter(FileName, Nothing)
NewXML.Formatting = Formatting.Indented
NewXML.WriteStartDocument()
NewXML.WriteComment(My.Application.Info.AssemblyName & " Settings ")
NewXML.WriteStartElement(Root)
NewXML.WriteAttributeString( "Powered ", "Null ")
NewXML.WriteEndElement()
NewXML.WriteEndDocument()
NewXML.Flush()
NewXML.Close()
Catch ex As Exception
Debug.Print( "Create - " & ex.Message)
Return False
Finally
If NewXML IsNot Nothing Then
NewXML.Close()
NewXML = Nothing
End If
End Try
Return True
End Function

Public Function Save(ByVal aSection As String, ByVal aKey As String, ByVal aValue As String) As Boolean
Dim Paths() As String
Dim n As Integer
Dim Node, Node2 As XmlNode
Dim Ele As XmlElement

While Strings.Left(aSection, 1) = "/ "
aSection = Strings.Mid(aSection, 2)
End While

'段名是否为空
If aSection = " " Then
XmlDoc.DocumentElement.RemoveAll()
Else
Paths = Strings.Split(aSection, "/ ")
Try
Node = XmlDoc.DocumentElement.SelectSingleNode(Paths(n))
If Node Is Nothing Then
Ele = XmlDoc.CreateElement(Paths(n))
Node = XmlDoc.DocumentElement.AppendChild(Ele)
End If
For n = 1 To Paths.Length - 1
If Paths(n) = " " Then Continue For
Node2 = Node.SelectSingleNode(Paths(n))
If Node2 Is Nothing Then
Ele = XmlDoc.CreateElement(Paths(n))
Node2 = Node.AppendChild(Ele)
End If
Node = Node2
Next
'键名是否为空
If aKey = " " Then
Node.RemoveAll()
Else
Ele = Node.Item(aKey)
If Ele Is Nothing Then
Ele = XmlDoc.CreateElement(aKey)
Node.AppendChild(Ele)


End If
'值是否为空
If aValue = " " Then
Node.RemoveChild(Ele)
Else
Ele.InnerText = aValue
End If
End If
Catch ex As Exception
Debug.Print(ex.Message)
Return False
End Try
End If
XmlDoc.Save(XmlFile)
Return True
End Function

Public Function Read(ByVal aSection As String, ByVal aKey As String, Optional ByVal aDefaultValue As String = " ") As String
Dim Node As XmlNode
Node = XmlDoc.DocumentElement.SelectSingleNode(aSection & "/ " & aKey)
If Node Is Nothing Then Return aDefaultValue
Return Node.InnerText
End Function

End Class
[解决办法]
DataSet ds=new DataSet();
ds.ReadXml(strXml);
foreach(DataRow r in ds.Tables[0].Rows)
{
tree.Nodes.Add(r[0]);
DataRow[] rows=ds.Tables[0].Select( "group= ' "+r[0]+ " ' ");
foreach(DataRow row in rows)
tree.Nodes[tree.Nodes.Count-1].Nodes.Add(row[1]);
}
[解决办法]
32486802

热点排行