vb.net修改xml的简单介绍

vb.net中怎么创建xml文件并写数据

DataSet 和 DataTable都有现成的方法:WriteXml

成都创新互联是工信部颁发资质IDC服务器商,为用户提供优质的四川乐山服务器托管服务

DataTable tb = this.dataGridView1.DataSource as DataTable;

if(tb != null)

{

tb.WriteXml(@"C:\table.xml",true);

return;

}

DataView dv = this.dataGridView1.DataSource as DataView;

if(dv != null)

{

dv.Table.WriteXml(@"C:\table.xml",true);

return;

}

IList list = this.dataGridView1.DataSource as IList;

if(list != null)

{

//to do,如果是IList,就要你自己想办法导出了

//XmlDocument or XmlWriter都可以考虑

}

vb修改config(xml)文件

我这里有个vb xml类,你试着用用!你需要了解dom 和xpath方面的知识。

下面的代码需要在工程中添加引用: microsoft xml 6.0

'*********************************************************************************************

'mornxml.cls xml解析类

'AUTHOR Morn Woo

'完成时间 2011年4月18日

'版本 ver1.0 20110111,修正了部分错误

'*********************************************************************************************

Option Explicit

Option Compare Text

DefInt I

DefStr S

DefDate D

DefLng L

DefBool B

Dim mvarsXmlFile As String 'xmlFile属性内存变量

Dim mvarsXmlContent As String 'xmlContent属性内存变量

Dim mvarsXmlRoot As MSXML2.DOMDocument 'xmlRoot属性内存变量

Public Property Let XmlRoot(ByRef vData As MSXML2.DOMDocument)

Set mvarsXmlRoot = vData

End Property

Public Property Get XmlRoot() As MSXML2.DOMDocument

Set XmlRoot = mvarsXmlRoot

End Property

Public Property Let XmlFile(ByVal vData As String)

mvarsXmlFile = vData

End Property

Public Property Get XmlFile() As String

XmlFile = mvarsXmlFile

End Property

Public Property Let XmlContent(ByVal vData As String)

mvarsXmlContent = vData

End Property

Public Property Get XmlContent() As String

XmlContent = mvarsXmlContent

End Property

'类初始化

Private Sub Class_Initialize()

Me.XmlContent = ""

Me.XmlFile = ""

'Me.XmlRoot = New MSXML2.DOMDocument

Set mvarsXmlRoot = New MSXML2.DOMDocument

End Sub

'Private Sub Class_Terminate()

' XmlRoot.abort

'

'End Sub

Function fun_XmlLoad(ByVal sFilePath As String) As Boolean

On Error GoTo Morn

fun_XmlLoad = False

If Dir(sFilePath, vbNormal) = "" Then Exit Function

Me.XmlRoot.Load sFilePath

Me.XmlFile = Trim(sFilePath)

Do While XmlRoot.readyState 4

DoEvents

Loop

Me.XmlFile = XmlRoot.XML

fun_XmlLoad = True

Exit Function

Morn:

End Function

'找到节点对象

Function fun_GetElement(ByVal sItem As String) As MSXML2.IXMLDOMElement

'Set fun_GetElement = New ms

On Error GoTo Morn

Set fun_GetElement = Me.XmlRoot.selectSingleNode(sItem)

Exit Function

Morn:

End Function

'读取节点text函数

Function fun_GetElementText(ByVal sItem As String) As String

Dim oElement As MSXML2.IXMLDOMElement

fun_GetElementText = ""

On Error GoTo Morn

Set oElement = Me.XmlRoot.selectSingleNode(sItem)

If oElement Is Nothing Then Exit Function

fun_GetElementText = oElement.Text

Exit Function

Morn:

End Function

'读取节点属性值函数,默认读取value属性值

Function fun_GetProperty(ByVal sItem As String, Optional ByVal sPropertyName = "value") As String

Dim oElement As MSXML2.IXMLDOMElement

fun_GetProperty = ""

On Error GoTo Morn

Set oElement = Me.XmlRoot.selectSingleNode(sItem)

Set oElement = Me.XmlRoot.selectSingleNode(sItem)

If oElement Is Nothing Then Exit Function

fun_GetProperty = oElement.getAttribute(sPropertyName)

Exit Function

Morn:

End Function

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

参考:

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问题!

Dim xmlDoc As New System.Xml.XmlDocument

xmlDoc.Load("c:\xml.xml") '载入xml文件

Dim Items As Xml.XmlNodeList = xmlDoc.DocumentElement.SelectNodes("//record/item") '参数为xpath查询串,前面斜杠,//:表示任何结点,/:表示根结点

For Each s As Xml.XmlNode In Items

Console.WriteLine(s.Attributes.GetNamedItem("id").Value  vbTab  s.InnerText)

Next

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.net修改xml的简单介绍
标题路径:http://cdiso.cn/article/dooejjc.html

其他资讯