1 / 13

Simple XML in .NET

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:

wei
Download Presentation

Simple XML in .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 <?xmlversion="1.0" encoding="UTF-8"?> <family> <namegender="Male" age="35"> <firstname>Tom</firstname> <lastname>Smith</lastname> </name> <namegender="Female" age="25"> <firstname>Dale</firstname> <lastname>Smith</lastname> </name> </family>

  5. XML structure in .NET Attribute name (color red) Element Node Name (color blue) 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 AsXmlDocument = 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 AsXmlTextReader = NewXmlTextReader("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 AsXmlDocument = New XmlDocument xmldoc.Load("example.xml") Dim nodelist = xmldoc.SelectNodes("/family/name") ForEach i AsXmlNodeIn nodelist ListBox1.Items.Add(i.Attributes("age").Value) Next EndSub

  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 ImportsSystem.Xml SubloadXMLtoDS() Dim ds AsNewDataSet ds.ReadXml("example.xml") DataGridView1.DataSource = ds.Tables(0) EndSub

  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 SubwriteXML() Dim conn AsNewSqlConnection("Data Source=localhost;Database=NorthWind;Integrated Security=True") Dim da AsNewSqlDataAdapter("select * from product", conn) Dim ds AsNewDataSet da.Fill(ds) ds.WriteXml("product.xml") EndSub

  10. Write XML file using XmlTextWriter PrivateSub Form1_Load(sender As Object, e AsEventArgs) 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() EndSub Sub createNode(ByVal pID As String, ByVal pName AsString, ByVal pPrice AsString, ByVal writer AsXmlTextWriter) 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 EndSub

  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 AsNewDataSet 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") EndIf EndSub

  12. Reference http://vb.net-informations.com/ www.codeproject.com www.tutorialspoint.com www.msdn.microsoft.com

  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

More Related