如何操作xml文件(对XML文件编辑:建立xml文件。增加、修改和删除节点--》VB)
是这样的,功能比如:
一个学生信息录入界面,录入基本信息,存入一个xml文件当中。
可以按顺序读出所存学生记录,也可以根据学号查出学生记录信息,也可以根据学号删除某学生的一条记录
我是刚接触VB.NET,希望得到大家的帮助
问题点数:100、回复次数:10Top
1 楼JzeroBiao(先知)回复于 2005-08-17 15:04:15 得分 5
DataSet.WriteXml
DataSet.GetXml
DataSet.ReadXml
-------------------------
System.XmlTop
2 楼kw123(阿柯)回复于 2005-08-18 18:15:35 得分 0
没有其他的意见吗?希望得到详细的回答,我是新手啊!Top
3 楼Tony_lau111082(刘寓)回复于 2005-08-18 19:25:14 得分 10
用XmlDocument
给你段读取和保存的例子吧
/// <summary>
/// 读取配置文件
/// </summary>
private void readConfig()
{
try
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode node;
xmlDoc.Load("showCfg.xml");
string path = "showCfg/net";
for (int i = 1;;i++)
{
path = path + i.ToString();
node = xmlDoc.SelectSingleNode(path);
if (node != null)
{
netName.Add(node.InnerText);
path = "showCfg/net";
}
else
{
break;
}
}
node = xmlDoc.SelectSingleNode("showCfg/color");
color = node.InnerText;
}
catch (Exception exp)
{
Debug.WriteLine(exp.Message);
Debug.WriteLine(exp.StackTrace);
Debug.WriteLine(exp.TargetSite.Name);
}Top
4 楼wqdzgh(红螃蟹)回复于 2005-08-18 19:30:28 得分 2
你的问题不好回答.你还是先找一些xml基础操作的教程练练吧.网上有很多.Top
5 楼chendazhi(不务正业)回复于 2005-08-18 19:40:30 得分 5
http://bbs.xml.org.cn/index.aspTop
6 楼ekinwt(沧海依粟)回复于 2005-08-18 19:49:46 得分 5
呵呵..就用XML来保存数据来说.有非常多种方法,比如说用XML的序列化.用ADO.net的XML功能.再有就是DOOM啦....这些技术在MSDN都是相关的例子,你可以参考一下...Top
7 楼zhilunchen(他山居士)回复于 2005-08-18 19:49:58 得分 3
先找本XML的书来读读吧.Top
8 楼dalianu(昵称一定要写的很长,即使人长丑点也要让人觉得有气派!)回复于 2005-08-19 09:20:43 得分 40
没有他们说的那么高深,别听他们的^_^
其实你要深入研究的确很要命,但是只是操作很简单,都是流操作:)
---------------------------------------------------------------------------------------
'保存配置XML文件表
Public Function SetConfigTable(ByVal pFileName As String, ByRef pTable As DataTable) As Boolean
Dim strPath As String
Dim ds As New DataSet
Dim fileStrm As System.IO.FileStream
Dim xmlStrm As System.Xml.XmlWriter
Try
If pTable Is Nothing Then
Return False
Else
ds.Tables.Add(pTable.Copy)
strPath = Application.StartupPath + CONFIGPATH + pFileName
fileStrm = New System.IO.FileStream(strPath, IO.FileMode.Create)
xmlStrm = New System.Xml.XmlTextWriter(fileStrm, System.Text.Encoding.Unicode)
ds.WriteXml(xmlStrm)
ds.Dispose()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
fileStrm.Close()
End Try
End Function
=========================================================================================
'取得配置XML文件表
Public Function GetConfigTable(ByVal pFileName As String, ByRef pCfgTable As DataTable) As Boolean
Dim path As String
Dim fileStrm As System.IO.FileStream
Dim xmlReader As System.Xml.XmlTextReader
Dim tpDs As New DataSet
Try
path = Application.StartupPath + CONFIGPATH + pFileName
fileStrm = New System.IO.FileStream(path, IO.FileMode.Open)
xmlReader = New System.Xml.XmlTextReader(fileStrm)
tpDs.ReadXml(xmlReader)
pCfgTable = tpDs.Tables(0).Copy
Catch ex As Exception
MessageBox.Show(ex.Message, "GetConfigTable", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
fileStrm.Close()
End Try
End Function
---------------------------------------------------------------------------
你要的操作其实就
写:
fileStrm = New System.IO.FileStream(strPath, IO.FileMode.Create)
xmlStrm = New System.Xml.XmlTextWriter(fileStrm, System.Text.Encoding.Unicode)
ds.WriteXml(xmlStrm)
读:
fileStrm = New System.IO.FileStream(path, IO.FileMode.Open)
xmlReader = New System.Xml.XmlTextReader(fileStrm)
tpDs.ReadXml(xmlReader)
这么一点^_^(高手别打我....怕怕)
用XmlDocument也可以.我分不清区别....有人讲讲真正意义上的区别吗?
Top
9 楼malingxian(Mask)回复于 2005-08-19 10:06:08 得分 30
Public Class XML
Private Shared _FileName As String
Public Shared Property FileName() As String
Get
Return _FileName
End Get
Set(ByVal Value As String)
_FileName = Value
End Set
End Property
Public Sub New(ByVal FileName As String)
_FileName = FileName
End Sub
Public Shared Function ExistFile() As Boolean
Return System.IO.File.Exists(_FileName)
End Function
'从XML中读取数据集
Public Shared Function Read() As DataSet
If Not ExistFile() Then
Throw New Exception("XML文件:" & _FileName & " 不存在!")
Return Nothing
End If
Dim tmpXMLRead As System.IO.FileStream
Dim tmpXMLReader As System.Xml.XmlTextReader
Try
tmpXMLRead = New System.IO.FileStream(_FileName, System.IO.FileMode.Open, IO.FileAccess.Read)
tmpXMLReader = New System.Xml.XmlTextReader(tmpXMLRead)
Dim tmpDataset As New DataSet("XML File")
tmpDataset.ReadXml(tmpXMLReader)
tmpXMLReader.Close()
tmpXMLRead.Close()
Return tmpDataset
Catch ex As Exception
If Not tmpXMLReader Is Nothing Then tmpXMLReader.Close()
If Not tmpXMLRead Is Nothing Then tmpXMLRead.Close()
Throw ex
End Try
End Function
'写数据集到XML中
Public Shared Sub Write(ByVal XMLDataSet As DataSet)
If XMLDataSet Is Nothing Then Throw New Exception("源数据流不存在!")
Dim tmpXMLWrite As System.IO.FileStream
Dim tmpXMLWriter As System.Xml.XmlTextWriter
Try
tmpXMLWrite = New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
tmpXMLWriter = New System.Xml.XmlTextWriter(tmpXMLWrite, System.Text.UTF8Encoding.UTF8)
tmpXMLWriter.Namespaces = True
tmpXMLWriter.Formatting = System.Xml.Formatting.Indented
tmpXMLWriter.IndentChar = vbTab
tmpXMLWriter.WriteStartDocument() '书写版本为“1.0”的 XML 声明。
XMLDataSet.WriteXml(tmpXMLWriter, XmlWriteMode.WriteSchema)
tmpXMLWriter.Close()
tmpXMLWrite.Close()
Catch ex As Exception
If Not tmpXMLWrite Is Nothing Then tmpXMLWrite.Close()
If Not tmpXMLWriter Is Nothing Then tmpXMLWriter.Close()
Throw ex
End Try
End Sub
Public Shared Sub ReadXLS()
Dim xsltf As New System.Xml.Xsl.XslTransform
Dim xslFile As New System.IO.StringWriter
Dim xslData As String 'XSL 字符串数据
Dim xmlData As String 'XML 字符串数据
xslFile.Write(xslData)
xsltf.Load(xslFile, New System.Xml.XmlUrlResolver, Nothing) '读取XSL样式
Dim xmlStream As New System.IO.MemoryStream
Dim xmlbuff As Byte() = System.Text.Encoding.Default.GetBytes(xmlData)
xmlStream.Write(xmlbuff, 0, xmlbuff.Length)
Dim xmlDoc As New System.Xml.XPath.XPathDocument(xmlStream)
Dim resolver As System.Xml.XmlUrlResolver = New System.Xml.XmlUrlResolver
Dim xmlOut As System.Xml.XmlReader = xsltf.Transform(xmlDoc, Nothing, resolver)
Dim strOut As String = xmlOut.Value()
End Sub
End Class
Top
10 楼kw123(阿柯)回复于 2005-08-22 11:38:16 得分 0
谢谢大家,准备结帖Top




