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

1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMS
koolkampus
 
Database Modeling Using Entity.. Weak And Strong Entity Types
Database Modeling Using Entity.. Weak And Strong Entity TypesDatabase Modeling Using Entity.. Weak And Strong Entity Types
Database Modeling Using Entity.. Weak And Strong Entity Types
aakanksha s
 

What's hot (20)

1. Introduction to DBMS
1. Introduction to DBMS1. Introduction to DBMS
1. Introduction to DBMS
 
WSDL
WSDLWSDL
WSDL
 
Xml
XmlXml
Xml
 
Static and Dynamic webpage
Static and Dynamic webpageStatic and Dynamic webpage
Static and Dynamic webpage
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Simple object access protocol(soap )
Simple object access protocol(soap )Simple object access protocol(soap )
Simple object access protocol(soap )
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
XML
XMLXML
XML
 
Characteristic of dabase approach
Characteristic of dabase approachCharacteristic of dabase approach
Characteristic of dabase approach
 
Active x control
Active x controlActive x control
Active x control
 
Database : Relational Data Model
Database : Relational Data ModelDatabase : Relational Data Model
Database : Relational Data Model
 
Database Modeling Using Entity.. Weak And Strong Entity Types
Database Modeling Using Entity.. Weak And Strong Entity TypesDatabase Modeling Using Entity.. Weak And Strong Entity Types
Database Modeling Using Entity.. Weak And Strong Entity Types
 
two tier and three tier
two tier and three tiertwo tier and three tier
two tier and three tier
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
 
ER Model in DBMS
ER Model in DBMSER Model in DBMS
ER Model in DBMS
 
Java Beans
Java BeansJava Beans
Java Beans
 
Sgml
SgmlSgml
Sgml
 
Client Server Architecture ppt
Client Server Architecture pptClient Server Architecture ppt
Client Server Architecture ppt
 
Html frames
Html framesHtml frames
Html frames
 

Viewers also liked

ASP.Net Presentation Part2
ASP.Net Presentation Part2ASP.Net Presentation Part2
ASP.Net Presentation Part2
Neeraj Mathur
 
2 dtd - validating xml documents
2   dtd - validating xml documents2   dtd - validating xml documents
2 dtd - validating xml documents
gauravashq
 
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
Randy Pilkenton
 
Active Server Page(ASP)
Active Server Page(ASP)Active Server Page(ASP)
Active Server Page(ASP)
Keshab Nath
 
Dotnet framework
Dotnet frameworkDotnet framework
Dotnet framework
Nitu Pandey
 
Concepts of Asp.Net
Concepts of Asp.NetConcepts of Asp.Net
Concepts of Asp.Net
vidyamittal
 

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

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

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