向XML中导不进数据? ( 积分: 200 )

F

ff_ff

Unregistered / Unconfirmed
GUEST, unregistred user!
Dim objxml As New System.Xml.XmlDocument
Dim objElement As System.Xml.XmlElement
Dim objText As System.Xml.XmlText
Dim objuser As System.Xml.XmlElement
objxml.Load(Server.MapPath(".") &
"/Operator.xml")
objuser = objxml.CreateElement("reguser")
' 用户名
objElement = objxml.CreateElement("username")
objText = objxml.CreateTextNode(TextBox1.Text)
objElement.AppendChild(objText)
objuser.AppendChild(objElement)
哪位大哥给讲讲这几段代码
 
F

ff_ff

Unregistered / Unconfirmed
GUEST, unregistred user!
Dim objxml As New System.Xml.XmlDocument
Dim objElement As System.Xml.XmlElement
Dim objText As System.Xml.XmlText
Dim objuser As System.Xml.XmlElement
objxml.Load(Server.MapPath(".") &
"/Operator.xml")
objuser = objxml.CreateElement("reguser")
' 用户名
objElement = objxml.CreateElement("username")
objText = objxml.CreateTextNode(TextBox1.Text)
objElement.AppendChild(objText)
objuser.AppendChild(objElement)
哪位大哥给讲讲这几段代码
 
F

ff_ff

Unregistered / Unconfirmed
GUEST, unregistred user!
S

suncheng_hong

Unregistered / Unconfirmed
GUEST, unregistred user!
转载
如何用ADO.NET操作XML
已知有一个XML文件(bookstore.xml)如下:
<?xml version=”1.0” encoding=”gb2312”?>
<bookstore>
<book genre=”fantasy” ISBN=”2-3631-4>
<title>Oberon’s Leqacy</title>
<author>Corets,Eva</author>
<pricr>5.95</price>
</book>
</bookstore>
1、往<bookstore>结点中插入一个<book>结点:
Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument '新建一个XML文档
xmlDoc.Load(&quot;../bookstore.xml&quot;) '加载指定的XML 数据
Dim root As Xml.XmlNode '声明 XML 文档中的单个节点
root = xmlDoc.SelectSingleNode(&quot;bookstore&quot;) '查找<bookstore>
If root Is Nothing then
MessageBox.Show(&quot;cuowu&quot;)
End If
Dim xe1 As Xml.XmlElement '声明一个元素
xe1 = xmlDoc.CreateElement(&quot;book&quot;) '创建一个<book>节点
xe1.SetAttribute(&quot;genre&quot;, &quot;李赞红&quot;) '设置该节点genre属性
xe1.SetAttribute(&quot;ISBN&quot;, &quot;2-3631-4&quot;) '设置该节点ISBN属性
Dim xesub1 As Xml.XmlElement
xesub1 = xmlDoc.CreateElement(&quot;title&quot;)
xesub1.InnerText = &quot;CS从入门到精通&quot;
'设置文本结点
xe1.AppendChild(xesub1) '添加到<book>结点中
Dim xesub2 As Xml.XmlElement
xesub2 = xmlDoc.CreateElement(&quot;author&quot;)
xesub2.InnerText = &quot;侯捷&quot;
xe1.AppendChild(xesub2)
Dim xesub3 As Xml.XmlElement
xesub3 = xmlDoc.CreateElement(&quot;price&quot;)
xesub3.InnerText = &quot;58.3&quot;
xe1.AppendChild(xesub3)
Try
root.AppendChild(xe1) '添加到<bookstore>结点中
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
xmlDoc.Save(&quot;../bookstore.xml&quot;)
--结果为:------------------------------------------------------------------------------------------------------------
<?xml version=&quot;1.0&quot;
encoding=&quot;gb2312&quot;?>
<bookstore>
<book genre=&quot;fantasy&quot;
ISBN=&quot;2-3631-4&quot;>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre=&quot;李赞红&quot;
ISBN=&quot;2-3631-4&quot;>
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument
xmlDoc.Load(&quot;../bookstore.xml&quot;)
Dim nodeList As Xml.XmlNodeList
'获取bookstore结点的所有子结点
nodeList = xmlDoc.SelectSingleNode(&quot;bookstore&quot;).ChildNodes
Dim xn As Xml.XmlNode
'遍历所有子结点
For Each xn In nodeList
Dim xe As Xml.XmlElement
xe = xn '将子节点类型转换为XmlElement类型
If xe.GetAttribute(&quot;genre&quot;) = &quot;李赞红&quot;
then
'如果genre属性为“李赞红”
xe.SetAttribute(&quot;genre&quot;, &quot;update李赞红&quot;) '则修改该属性为“update李赞红”
Dim nls As Xml.XmlNodeList
nls = xe.ChildNodes '继续获取xe子节点的所有子节点
Dim xnl As Xml.XmlNode
For Each xnl In nls '遍历
Dim xe2 As Xml.XmlElement
xe2 = xnl '转换类型
If xe2.Name = &quot;author&quot;
then
'如果找到
xe2.InnerText = &quot;亚胜&quot;
'则修改
Exit For '退出循环
End If
Next
Exit For
End If
Next
xmlDoc.Save(&quot;../bookstore.xml&quot;) '保存
----结果为:----------------------------------------------------------------------------------------------------
<?xml version=&quot;1.0&quot;
encoding=&quot;gb2312&quot;?>
<bookstore>
<book genre=&quot;fantasy&quot;
ISBN=&quot;2-3631-4&quot;>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre=&quot;update李赞红&quot;
ISBN=&quot;2-3631-4&quot;>
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>
3、删除 <book genre=&quot;fantasy&quot;
ISBN=&quot;2-3631-4&quot;>节点的genre属性,删除 <book genre=&quot;update李赞红&quot;
ISBN=&quot;2-3631-4&quot;>节点。
Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument
xmlDoc.Load(&quot;../bookstore.xml&quot;)
Dim nodeList As Xml.XmlNodeList
'获取bookstore结点的所有子结点
nodeList = xmlDoc.SelectSingleNode(&quot;bookstore&quot;).ChildNodes
Dim xn As Xml.XmlNode
'遍历所有子结点
For Each xn In nodeList
Dim xe As Xml.XmlElement
xe = xn
If xe.GetAttribute(&quot;genre&quot;) = &quot;fantasy&quot;
then
xe.RemoveAttribute(&quot;genre&quot;) '删除genre属性
else
If xe.GetAttribute(&quot;genre&quot;) = &quot;update李赞红&quot;
then
xe.RemoveAll() '删除该节点的全部内容
End If
Next
xmlDoc.Save(&quot;../bookstore.xml&quot;)
----结果为:----------------------------------------------------------------------------------------------------------
<?xml version=&quot;1.0&quot;
encoding=&quot;gb2312&quot;?>
<bookstore>
<book ISBN=&quot;2-3631-4&quot;>
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>
4、显示所有数据
Dim xmlDoc As New Xml.XmlDocument
xmlDoc.Load(&quot;../bookstore.xml&quot;)
Dim xn As Xml.XmlNode
xn = xmlDoc.SelectSingleNode(&quot;bookstore&quot;)
Dim xnl As Xml.XmlNodeList
xnl = xn.ChildNodes
Dim xnf As Xml.XmlNode
For Each xnf In xnl
Dim xe As Xml.XmlElement
xe = xnf
txtbook.Text &amp;= xe.GetAttribute(&quot;genre&quot;) &amp;
vbCrLf 'Console.WriteLine(xe.GetAttribute(&quot;genre&quot;))
txtbook.Text &amp;= xe.GetAttribute(&quot;ISBN&quot;) &amp;
vbCrLf 'Console.WriteLine(xe.GetAttribute(&quot;ISBN&quot;))
Dim xnfl As Xml.XmlNodeList
xnfl = xe.ChildNodes
Dim xn2 As Xml.XmlNode
For Each xn2 In xnfl
txtbook.Text &amp;= xn2.InnerText &amp;
vbCrLf 'Console.WriteLine(xn2.InnerText)
Next
Next
5、读取单个数据
Dim xmlDoc As New Xml.XmlDocument
MessageBox.Show(Application.ExecutablePath)
xmlDoc.Load(&quot;../bookstore.xml&quot;) '(Application.ExecutablePath + &quot;/../bookstore.xml&quot;)
Dim tmpNode As Xml.XmlNode
Try
tmpNode = xmlDoc.Item(&quot;bookstore&quot;).ChildNodes(0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MessageBox.Show(tmpNode.Name)
If tmpNode.Name = &quot;book&quot;
then
MessageBox.Show(tmpNode.ChildNodes(0).InnerText)
MessageBox.Show(tmpNode.ChildNodes(1).InnerText)
MessageBox.Show(tmpNode.ChildNodes(2).InnerText)
End If
 
F

ff_ff

Unregistered / Unconfirmed
GUEST, unregistred user!
顶部