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

JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object ModelWebStackAcademy
 
Data Manipulation Language
Data Manipulation LanguageData Manipulation Language
Data Manipulation LanguageJas Singh Bhasin
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptMarlon Jamera
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentationNITISH KUMAR
 
Lecture 1 introduction to vb.net
Lecture 1   introduction to vb.netLecture 1   introduction to vb.net
Lecture 1 introduction to vb.netMUKALU STEVEN
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and DesignHaitham El-Ghareeb
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)WebStackAcademy
 
Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Netrishisingh190
 

What's hot (20)

JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Data Manipulation Language
Data Manipulation LanguageData Manipulation Language
Data Manipulation Language
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Lecture 1 introduction to vb.net
Lecture 1   introduction to vb.netLecture 1   introduction to vb.net
Lecture 1 introduction to vb.net
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
 
Visual Basic Controls ppt
Visual Basic Controls pptVisual Basic Controls ppt
Visual Basic Controls ppt
 
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
Exception Handling in VB.Net
Exception Handling in VB.NetException Handling in VB.Net
Exception Handling in VB.Net
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 

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

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 

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