Question read and write xml file

ak08

New member
Joined
Jan 14, 2009
Messages
4
Programming Experience
Beginner
Hi friends,

I am new to Vb.net programming. I am developing a project.
Coule anyone please help me with the code of how to write and read a xml file from VB.net and if possible also in ASP.net

I am using C# wiht VB and ASP.

I am also using SQL server.

Thanks in advance
 
You can't be using C# with VB. They are mutually exclusive. ASP.NET is a technology for building applications for the web and you can write the code-behind in either C# or VB. This forum is for VB only, not C#. If you want help with the VB language then you should post in the VB forums, not the VS forums, which is for IDE issues. If you want help with C# then I'm afraid you'll need to find a site dedicated to C#.
 
Create Xml file in VB.net

You can write xml file using this code.

:):):):):)
Imports System.Data.SqlClient
Imports System.IO

Dim con As New SqlConnection
Dim cmd As SqlCommand

Dim DataAdptr As SqlDataAdapter
Dim DtaSet As New DataSet

Public Function Connection() As SqlConnection
Try
If con.State = ConnectionState.Open Then con.Close()
con = New SqlConnection("Data Source=IPAddress;Database=DatabaseName;uid=sa;pwd=;")
con.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return con
End Function

private sub toCreateXmlFile()
cmd = New SqlCommand("select * from Table1", Connection())
cmd.CommandType = CommandType.Text
DataAdptr = New SqlDataAdapter(cmd)
DataAdptr.Fill(DtaSet, "Table1")

cmd = New SqlCommand("select * from Table2", Connection())
cmd.CommandType = CommandType.Text
DataAdptr = New SqlDataAdapter(cmd)
DataAdptr.Fill(DtaSet, "Table2")


DtaSet.WriteXml(Application.StartupPath & "\Myfile.xml", XmlWriteMode.WriteSchema)
Dim StrmWritr As StreamWriter = New StreamWriter(Application.StartupPath & "\Myfile.xml", XmlWriteMode.WriteSchema)
DtaSet.WriteXml(StrmWritr, XmlWriteMode.WriteSchema)
StrmWritr.Flush()
StrmWritr.Close()
DtaSet.Clear()

end sub
 
Read from xml file in VB.net

For example: our xml file look like this

<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table1">
<xs:complexType>
<xs:sequence>
<xs:element name="Column1" type="xs:string" minOccurs="0" />
<xs:element name="Column2" type="xs:int" minOccurs="0" />
<xs:element name="Column3" type="xs:int" minOccurs="0" />
<xs:element name="Column4" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Table2">
<xs:complexType>
<xs:sequence>
<xs:element name="Column10" type="xs:int" minOccurs="0" />
<xs:element name="Column11" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table1>
<Column1>test</Column1>
<Column2>1</Column2>
<Column3>2</Column3>
<Column4>testing</Column4>
</Table1>
<Table2>
<Column10>22</Column10>
<Column11>11</Column11>
</Table2>
</NewDataSet>






Imports System.Xml.XPath
Imports System.Xml
Imports System.IO


Dim doc As XPathDocument
Dim nav As XPathNavigator
Dim expr As XPathExpression
Dim xr As XmlReader
Dim iterator As XPathNodeIterator

private sub toReadXmlFile()

try
doc = New XPathDocument(Application.StartupPath & "\myfile.xml")
nav = doc.CreateNavigator
expr = nav.Compile("/NewDataSet/Table1")
iterator = nav.Select(expr)
Dim larrayOffer(5) As String
While iterator.MoveNext = True
Dim nav2 As XPathNavigator = iterator.Current.Clone()
nav2.MoveToFirstChild()
nav2.MoveToNext()
larrayOffer(0) = nav2.Value 'Column1
nav2.MoveToNext()
larrayOffer(1) = nav2.Value 'Column2
nav2.MoveToNext()
larrayOffer(2) = nav2.Value 'Column3
nav2.MoveToNext()
larrayOffer(3) = nav2.Value ' 'Column4

Insert function to SQL here.


End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub



:):):):):):):)
 

Latest posts

Back
Top