SlideShare a Scribd company logo
1 of 13
Simple XML in .NET
This presentation will introduction XML
and using XML in .NET by simplest way
(C# and VB.NET)
Introduction
XML is Extensible Markup Language. It is include tag
defined by user and text inside the tag. The tag begin with <
and end with >, tag have three type:
•start-tags; for example: <section>
•end-tags; for example: </section>
•empty-element tags; for example: <line-break />

The tag was defined by user with any name, you can learn
more on Internet.
Introduction
Because the tag can be defined by user so it can
be used as database, or setting of program, etc,...
And this presentation will intro simplest way to
read/write on XML document.
File XML to example
In this presentation i will use this XML file to example
<?xml version="1.0" encoding="UTF-8"?>
<family>
<name gender="Male" age="35">
<firstname>Tom</firstname>
<lastname>Smith</lastname>
</name>
<name gender="Female" age="25">
<firstname>Dale</firstname>
<lastname>Smith</lastname>
</name>
</family>
XML structure in .NET
Element
Node
Name
(color blue)

Attribute name
(color red)

Attribute value
(In double quote)

Inner text
Value
Read value in XML file
Read Firstname (you must add Listbox control to form)
Sub readValuexml ()
Dim xmldoc As XmlDocument = New XmlDocument
xmldoc.Load("example.xml")
Dim nodelist = xmldoc.SelectNodes("/family/name")
For Each i As XmlNode In nodelist
ListBox1.Items.Add(i("firstname").InnerText)
Next
End Sub

Anotherway
Imports System.Xml
Sub readValuexml ()
Dim xmltext As XmlTextReader = New XmlTextReader("example.xml")
Do While xmltext.Read
If xmltext.Name = "firstname" Then
ListBox1.Items.Add(xmltext.ReadElementString)
End If
Loop
End Sub
Read attribute value in XML file
Read attribute Age, return 35 and 25
Sub readAttributeXML()
Dim xmldoc As XmlDocument = New XmlDocument
xmldoc.Load("example.xml")
Dim nodelist = xmldoc.SelectNodes("/family/name")
For Each i As XmlNode In nodelist
ListBox1.Items.Add(i.Attributes("age").Value)
Next
End Sub
Read data from XML file to Dataset
You must add DataGridView to form. This code will read
data from XML file and show in DataGridView

Imports System.Xml

Sub loadXMLtoDS()
Dim ds As New DataSet
ds.ReadXml("example.xml")
DataGridView1.DataSource = ds.Tables(0)
End Sub
Write XML file from Dataset
You can write XML file very easy . The first, you get data
from Database to Dataset, and write it to XML file
Sub writeXML()
Dim conn As New SqlConnection("Data
Source=localhost;Database=NorthWind;Integrated Security=True")
Dim da As New SqlDataAdapter("select * from product", conn)
Dim ds As New DataSet
da.Fill(ds)
ds.WriteXml("product.xml")
End Sub
Write XML file using XmlTextWriter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim writer As New XmlTextWriter("product.xml", System.Text.Encoding.UTF8)
writer.WriteStartDocument(True)
writer.Formatting = Formatting.Indented
writer.Indentation = 2
writer.WriteStartElement("Table")
createNode(1, "Product 1", "1000", writer)
createNode(2, "Product 2", "2000", writer)
createNode(3, "Product 3", "3000", writer)
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()
End Sub

Sub createNode(ByVal pID As String, ByVal pName As String, ByVal pPrice As String, ByVal writer
As XmlTextWriter)
writer.WriteStartElement("Product")
writer.WriteStartElement("Product_id")
writer.WriteString(pID)
writer.WriteEndElement() 'end Product_id
writer.WriteStartElement("Product_name")
writer.WriteString(pName)
writer.WriteEndElement() 'end Product_name
writer.WriteStartElement("Product_price")
writer.WriteString(pPrice)
writer.WriteEndElement() 'end Product_price
writer.WriteEndElement() 'end Product
End Sub
Search
This code will read data from XML file to DataSet, and using Find() function of
DataSet to find data.

Sub findInXML()
Dim ds As New DataSet
ds.ReadXml("example.xml")
Dim resultRow = ds.Tables(0).Select("firstname = 'Dale'")
If resultRow.Count = 0 Then
MsgBox("Could not be found")
Else
MsgBox("Found")
End If
End Sub
Reference
1.
2.
3.
4.

http://vb.net-informations.com/
www.codeproject.com
www.tutorialspoint.com
www.msdn.microsoft.com
About
Because this is the first presentation should not avoid errors. I wellcome any
contribute to this presentation more complete.
Any question or contribute please send for me via email address:
vohungvi@vohungvi.com or facebook: fb.com/vohungvi

More Related Content

What's hot

What's hot (20)

Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
The Relational Data Model and Relational Database Constraints Ch5 (Navathe 4t...
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
Introduction to JavaScript Basics.
Introduction to JavaScript Basics.Introduction to JavaScript Basics.
Introduction to JavaScript Basics.
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
 
Learn javascript easy steps
Learn javascript easy stepsLearn javascript easy steps
Learn javascript easy steps
 
Javascript
JavascriptJavascript
Javascript
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Dom
DomDom
Dom
 
Session tracking in servlets
Session tracking in servletsSession tracking in servlets
Session tracking in servlets
 
HTML5 DRAG AND DROP
HTML5 DRAG AND DROPHTML5 DRAG AND DROP
HTML5 DRAG AND DROP
 
Css selectors
Css selectorsCss selectors
Css selectors
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Html form tag
Html form tagHtml form tag
Html form tag
 
Introduction to DOM
Introduction to DOMIntroduction to DOM
Introduction to DOM
 
cascading style sheet ppt
cascading style sheet pptcascading style sheet ppt
cascading style sheet ppt
 
ASP.NET Web form
ASP.NET Web formASP.NET Web form
ASP.NET Web form
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
 

Viewers also liked

ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1Kumar S
 
ASP.Net Presentation Part2
ASP.Net Presentation Part2ASP.Net Presentation Part2
ASP.Net Presentation Part2Neeraj Mathur
 
Taming Rich GML with Stetl - FOSS4G 2013 Nottingham
Taming Rich GML with Stetl - FOSS4G 2013 NottinghamTaming Rich GML with Stetl - FOSS4G 2013 Nottingham
Taming Rich GML with Stetl - FOSS4G 2013 NottinghamJust van den Broecke
 
SimpleXML In PHP 5
SimpleXML In PHP 5SimpleXML In PHP 5
SimpleXML In PHP 5Ron Pringle
 
2 dtd - validating xml documents
2   dtd - validating xml documents2   dtd - validating xml documents
2 dtd - validating xml documentsgauravashq
 
Introduction to asp .net
Introduction to asp .netIntroduction to asp .net
Introduction to asp .netumesh patil
 
E billing and invoice system
E billing and invoice systemE billing and invoice system
E billing and invoice systemSurya Indira
 
Electronic Billing And Payment Market, After All These Years
Electronic Billing And Payment Market, After All These YearsElectronic Billing And Payment Market, After All These Years
Electronic Billing And Payment Market, After All These YearsRandy Pilkenton
 
Electronic Bill & Payment
Electronic Bill & PaymentElectronic Bill & Payment
Electronic Bill & PaymentJesus Hoyos
 
Active Server Page(ASP)
Active Server Page(ASP)Active Server Page(ASP)
Active Server Page(ASP)Keshab Nath
 
Ebilling project report
Ebilling project reportEbilling project report
Ebilling project reportSrish Kumar
 
Dotnet framework
Dotnet frameworkDotnet framework
Dotnet frameworkNitu Pandey
 
Intro To Asp Net And Web Forms
Intro To Asp Net And Web FormsIntro To Asp Net And Web Forms
Intro To Asp Net And Web FormsSAMIR BHOGAYTA
 
Concepts of Asp.Net
Concepts of Asp.NetConcepts of Asp.Net
Concepts of Asp.Netvidyamittal
 

Viewers also liked (20)

ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1
 
ASP.Net Presentation Part2
ASP.Net Presentation Part2ASP.Net Presentation Part2
ASP.Net Presentation Part2
 
Taming Rich GML with Stetl - FOSS4G 2013 Nottingham
Taming Rich GML with Stetl - FOSS4G 2013 NottinghamTaming Rich GML with Stetl - FOSS4G 2013 Nottingham
Taming Rich GML with Stetl - FOSS4G 2013 Nottingham
 
SimpleXML In PHP 5
SimpleXML In PHP 5SimpleXML In PHP 5
SimpleXML In PHP 5
 
Introduction to asp
Introduction to aspIntroduction to asp
Introduction to asp
 
2 dtd - validating xml documents
2   dtd - validating xml documents2   dtd - validating xml documents
2 dtd - validating xml documents
 
Introduction to asp .net
Introduction to asp .netIntroduction to asp .net
Introduction to asp .net
 
E billing and invoice system
E billing and invoice systemE billing and invoice system
E billing and invoice system
 
Introduction ASP
Introduction ASPIntroduction ASP
Introduction ASP
 
Electronic Billing And Payment Market, After All These Years
Electronic Billing And Payment Market, After All These YearsElectronic Billing And Payment Market, After All These Years
Electronic Billing And Payment Market, After All These Years
 
Electronic Bill & Payment
Electronic Bill & PaymentElectronic Bill & Payment
Electronic Bill & Payment
 
Active Server Page(ASP)
Active Server Page(ASP)Active Server Page(ASP)
Active Server Page(ASP)
 
Ebilling project report
Ebilling project reportEbilling project report
Ebilling project report
 
Dotnet framework
Dotnet frameworkDotnet framework
Dotnet framework
 
Asp.net
 Asp.net Asp.net
Asp.net
 
ASP
ASPASP
ASP
 
Intro To Asp Net And Web Forms
Intro To Asp Net And Web FormsIntro To Asp Net And Web Forms
Intro To Asp Net And Web Forms
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
Concepts of Asp.Net
Concepts of Asp.NetConcepts of Asp.Net
Concepts of Asp.Net
 
ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
 

Similar to Simple xml in .net (20)

Xml writers
Xml writersXml writers
Xml writers
 
Xml And JSON Java
Xml And JSON JavaXml And JSON Java
Xml And JSON Java
 
distributed system concerned lab sessions
distributed system concerned lab sessionsdistributed system concerned lab sessions
distributed system concerned lab sessions
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
Xml processing in scala
Xml processing in scalaXml processing in scala
Xml processing in scala
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Introduction to xml schema
Introduction to xml schemaIntroduction to xml schema
Introduction to xml schema
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
Xml
XmlXml
Xml
 
XML Presentation-2
XML Presentation-2XML Presentation-2
XML Presentation-2
 
XML
XMLXML
XML
 
Xml11
Xml11Xml11
Xml11
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
 
Unit 5 xml (1)
Unit 5   xml (1)Unit 5   xml (1)
Unit 5 xml (1)
 
XML notes.pptx
XML notes.pptxXML notes.pptx
XML notes.pptx
 
XML.ppt
XML.pptXML.ppt
XML.ppt
 
XML Schema.pptx
XML Schema.pptxXML Schema.pptx
XML Schema.pptx
 
Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
 
R-XML.docx
R-XML.docxR-XML.docx
R-XML.docx
 

Recently uploaded

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 

Simple xml in .net

  • 1. Simple XML in .NET This presentation will introduction XML and using XML in .NET by simplest way (C# and VB.NET)
  • 2. Introduction XML is Extensible Markup Language. It is include tag defined by user and text inside the tag. The tag begin with < and end with >, tag have three type: •start-tags; for example: <section> •end-tags; for example: </section> •empty-element tags; for example: <line-break /> The tag was defined by user with any name, you can learn more on Internet.
  • 3. Introduction Because the tag can be defined by user so it can be used as database, or setting of program, etc,... And this presentation will intro simplest way to read/write on XML document.
  • 4. File XML to example In this presentation i will use this XML file to example <?xml version="1.0" encoding="UTF-8"?> <family> <name gender="Male" age="35"> <firstname>Tom</firstname> <lastname>Smith</lastname> </name> <name gender="Female" age="25"> <firstname>Dale</firstname> <lastname>Smith</lastname> </name> </family>
  • 5. XML structure in .NET Element Node Name (color blue) Attribute name (color red) Attribute value (In double quote) Inner text Value
  • 6. Read value in XML file Read Firstname (you must add Listbox control to form) Sub readValuexml () Dim xmldoc As XmlDocument = New XmlDocument xmldoc.Load("example.xml") Dim nodelist = xmldoc.SelectNodes("/family/name") For Each i As XmlNode In nodelist ListBox1.Items.Add(i("firstname").InnerText) Next End Sub Anotherway Imports System.Xml Sub readValuexml () Dim xmltext As XmlTextReader = New XmlTextReader("example.xml") Do While xmltext.Read If xmltext.Name = "firstname" Then ListBox1.Items.Add(xmltext.ReadElementString) End If Loop End Sub
  • 7. Read attribute value in XML file Read attribute Age, return 35 and 25 Sub readAttributeXML() Dim xmldoc As XmlDocument = New XmlDocument xmldoc.Load("example.xml") Dim nodelist = xmldoc.SelectNodes("/family/name") For Each i As XmlNode In nodelist ListBox1.Items.Add(i.Attributes("age").Value) Next End Sub
  • 8. Read data from XML file to Dataset You must add DataGridView to form. This code will read data from XML file and show in DataGridView Imports System.Xml Sub loadXMLtoDS() Dim ds As New DataSet ds.ReadXml("example.xml") DataGridView1.DataSource = ds.Tables(0) End Sub
  • 9. Write XML file from Dataset You can write XML file very easy . The first, you get data from Database to Dataset, and write it to XML file Sub writeXML() Dim conn As New SqlConnection("Data Source=localhost;Database=NorthWind;Integrated Security=True") Dim da As New SqlDataAdapter("select * from product", conn) Dim ds As New DataSet da.Fill(ds) ds.WriteXml("product.xml") End Sub
  • 10. Write XML file using XmlTextWriter Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim writer As New XmlTextWriter("product.xml", System.Text.Encoding.UTF8) writer.WriteStartDocument(True) writer.Formatting = Formatting.Indented writer.Indentation = 2 writer.WriteStartElement("Table") createNode(1, "Product 1", "1000", writer) createNode(2, "Product 2", "2000", writer) createNode(3, "Product 3", "3000", writer) writer.WriteEndElement() writer.WriteEndDocument() writer.Close() End Sub Sub createNode(ByVal pID As String, ByVal pName As String, ByVal pPrice As String, ByVal writer As XmlTextWriter) writer.WriteStartElement("Product") writer.WriteStartElement("Product_id") writer.WriteString(pID) writer.WriteEndElement() 'end Product_id writer.WriteStartElement("Product_name") writer.WriteString(pName) writer.WriteEndElement() 'end Product_name writer.WriteStartElement("Product_price") writer.WriteString(pPrice) writer.WriteEndElement() 'end Product_price writer.WriteEndElement() 'end Product End Sub
  • 11. Search This code will read data from XML file to DataSet, and using Find() function of DataSet to find data. Sub findInXML() Dim ds As New DataSet ds.ReadXml("example.xml") Dim resultRow = ds.Tables(0).Select("firstname = 'Dale'") If resultRow.Count = 0 Then MsgBox("Could not be found") Else MsgBox("Found") End If End Sub
  • 13. About Because this is the first presentation should not avoid errors. I wellcome any contribute to this presentation more complete. Any question or contribute please send for me via email address: vohungvi@vohungvi.com or facebook: fb.com/vohungvi

Editor's Notes

  1. Use XmlTextReader will faster than XmlDocument, because XmlDocument will load all file Xml to memory
  2. This example i copy original from http://vb.net-informations.com/ because it perfect