利用vb对XML如何进行修改?
利用DataTable,读取XML,DataTable。ReadXML(Filepath),然后就可以像修改表格那样改了。
创新互联建站成立于2013年,先为赫章等服务建站,赫章等地企业,进行企业商务咨询服务。为赫章企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
VB.NET 或者 C#实现XML树增删改节点 代码
//创建XMLdocument
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
// 从XML文件中加载XML
doc.Load(XmlPath);
//为doc的根节点创建子节点nodeA(没有添加到根节点上!)
System.Xml.XmlNode nodeA = doc.CreateNode(System.Xml.XmlNodeType.Element, "SAMPLE_ADD", "SAMPLEURI_ADD");
//为子节点nodeA设置属性
nodeA.Value = "SAMPLE VALUE ADD";
//将nodeA添加为doc的子节点
doc.AppendChild(nodeA);
//为nodeA节点创建子节点nodeAA(没有添加到nodeA节点上!)
System.Xml.XmlNode nodeAA = nodeA.CreateNode(System.Xml.XmlNodeType.Element, "SAMPLE_ADD2", "SAMPLEURI_ADD2");
//为子节点nodeAA设置属性
nodeAA.Value = "SAMPLE VALUE ADD2";
//将nodeAA添加为nodeA的子节点
nodeA.AppendChild(nodeAA);
//遍历nodeA下面的所有子节点
foreach (System.Xml.XmlNode node in nodeA.ChildNodes)
{
//处理这些节点
}
//删除节点的做法是遍历该节点然后吧符合条件的删除掉
foreach (System.Xml.XmlNode node in doc.ChildNodes)
{
// 将节点从父上删除
doc.RemoveChild(node);
}
vb.net操作xml数据库(急)
使用System.XML
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Xml
namespace HowTo.Samples.XML
public class WriteXmlFileSample
private const document as string = "newbooks.xml"
shared sub Main()
Dim myWriteXmlFileSample as WriteXmlFileSample
myWriteXmlFileSample = new WriteXmlFileSample()
myWriteXmlFileSample.Run(document)
end sub
public sub Run(args As String)
Dim myXmlTextReader as XmlTextReader = nothing
Dim myXmlTextWriter as XmlTextWriter = nothing
try
myXmlTextWriter = new XmlTextWriter (args, nothing)
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartDocument(false)
myXmlTextWriter.WriteDocType("bookstore", nothing, "books.dtd", nothing)
myXmlTextWriter.WriteComment("此文件表示书店库存数据库的另一个片断")
myXmlTextWriter.WriteStartElement("bookstore")
myXmlTextWriter.WriteStartElement("book", nothing)
myXmlTextWriter.WriteAttributeString("genre","autobiography")
myXmlTextWriter.WriteAttributeString("publicationdate","1979")
myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9")
myXmlTextWriter.WriteElementString("title", nothing, "The Autobiography of Mark Twain")
myXmlTextWriter.WriteStartElement("Author", nothing)
myXmlTextWriter.WriteElementString("first-name", "Mark")
myXmlTextWriter.WriteElementString("last-name", "Twain")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteElementString("price", "7.99")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteEndElement()
'向文件写 XML 并关闭编写器
myXmlTextWriter.Flush()
myXmlTextWriter.Close()
' 读取返回的文件并进行分析以确保正确生成 XML
myXmlTextReader = new XmlTextReader (args)
FormatXml (myXmlTextReader, args)
catch e as Exception
Console.WriteLine ("异常:{0}", e.ToString())
finally
Console.WriteLine()
Console.WriteLine("对文件 {0} 的处理已完成。", args)
If Not myXmlTextReader Is Nothing
myXmlTextReader.Close()
end if
'关闭编写器
If Not myXmlTextWriter Is Nothing
myXmlTextWriter.Close()
end if
End try
End Sub
private shared Sub FormatXml (reader as XmlTextReader, filename as String)
Dim piCount, docCount, commentCount, elementCount as Integer
Dim attributeCount, textCount, whitespaceCount as Integer
While reader.Read()
Select (reader.NodeType)
case XmlNodeType.ProcessingInstruction:
Format (reader, "ProcessingInstruction")
piCount += 1
case XmlNodeType.DocumentType:
Format (reader, "DocumentType")
docCount += 1
case XmlNodeType.Comment:
Format (reader, "Comment")
commentCount += 1
case XmlNodeType.Element:
Format (reader, "Element")
elementCount += 1
While reader.MoveToNextAttribute()
Format (reader, "Attribute")
end While
if (reader.HasAttributes)
attributeCount += reader.AttributeCount
end if
case XmlNodeType.Text:
Format (reader, "Text")
textCount += 1
case XmlNodeType.Whitespace:
whitespaceCount += 1
End Select
End While
' 显示该文件的统计信息
Console.WriteLine ()
Console.WriteLine("{0} 文件的统计信息", filename)
Console.WriteLine ()
Console.WriteLine("处理指令:" piCount)
Console.WriteLine("文档类型:" docCount)
Console.WriteLine("注释:" commentCount)
Console.WriteLine("元素:" elementCount)
Console.WriteLine("属性:" attributeCount)
Console.WriteLine("文本:" textCount)
Console.WriteLine("空白:" whitespaceCount)
End Sub
private shared Sub Format(byref reader as XmlTextReader , NodeType as String)
' 格式化输出
Console.Write(reader.Depth " ")
Console.Write(reader.AttributeCount " ")
Dim i as Integer
for i = 0 to reader.Depth - 1
Console.Write(Strings.chr(9))
Next
Console.Write(reader.Prefix NodeType "" reader.Name "" reader.Value)
Console.WriteLine()
End Sub
End Class
End Namespace
参考:
VB6.0如何读取并修改XML文件?
你有两个选择:
1、将xml文件当做文本文件来处理。替换指定字符即可。
2、使用 MSXML 组件来处理。度娘搜一下,一大堆文档可参考。
打字不易,如满意,望采纳。
VB.NET修改替换xml文件中的值
Dim path As String = PDA_PATH "ife.XML" ’PDA_PATH 为路径
Dim ds As New DataSet
ds.ReadXml(path)
Dim dt As DataTable = ds.Tables.Item(0)
Dim blnY As Boolean = True
For Each row As DataRow In dt.Rows
If row.Item("Translation").ToString.ToUpper ="确认"Then
row.Item("Translation") =“替换”
blnY = False
Exit For
End If
Next
If blnY Then
MsgBox("输入的XXX不存在,请重新输入! ")
Return
End If
ds.WriteXml(path)
MessageBox.Show("修改数据并保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
或者
Dim xmlDoc As New XmlDocument()
'Dim nodeList As New XmlNodeList
xmlDoc.Load("life..xml") '加载
Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNod.("UITranslations").ChildNodes '获取bookstore节点的所有子节点
Dim xn As XmlNode
For Each xn In nodeList '遍历所有子节点
Dim xe As XmlElement = CType(xn, XmlElement) '将子节点类型转换为XmlElement类型
Dim nls As XmlNodeList = xe.ChildNodes '继续获取xe子节点的所有子节点
Dim xn1 As XmlNode
For Each xn1 In nls '遍历
Dim xe2 As XmlElement = CType(xn1, XmlElement) '转换类型
If xe2.Name = "Translation" Then '如果找到
xe2.InnerText ="替换"则修改
'Exit For Each '找到退出来就可以了
End If
Next xn1
Next xn
xmlDoc.Save("life.xml") '保存。
MessageBox.Show("修改XML成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
看能不能帮到你!
本文名称:vb.netxml修改的简单介绍
网页网址:http://lswzjz.com/article/hjeioo.html