Menu

VB.NET TUTORIALS - VB.Net - XML Processing

VB.Net - XML Processing

ADVERTISEMENTS

S.NClassDescription
1XmlAttributeRepresents an attribute. Valid and default values for the attribute are defined in a document type definition (DTD) or schema.
2XmlCDataSectionRepresents a CDATA section.
3XmlCharacterDataProvides text manipulation methods that are used by several classes.
4XmlCommentRepresents the content of an XML comment.
5XmlConvertEncodes and decodes XML names and provides methods for converting between common language runtime types and XML Schema definition language (XSD) types. When converting data types, the values returned are locale independent.
6XmlDeclarationRepresents the XML declaration node <?xml version='1.0'...?>.
7XmlDictionaryImplements a dictionary used to optimize Windows Communication Foundation (WCF)'s XML reader/writer implementations.
8XmlDictionaryReaderAn abstract class that the Windows Communication Foundation (WCF) derives from XmlReader to do serialization and deserialization.
9XmlDictionaryWriterRepresents an abstract class that Windows Communication Foundation (WCF) derives from XmlWriter to do serialization and deserialization.
10XmlDocumentRepresents an XML document.
11XmlDocumentFragmentRepresents a lightweight object that is useful for tree insert operations.
12XmlDocumentTypeRepresents the document type declaration.
13XmlElementRepresents an element.
14XmlEntityRepresents an entity declaration, such as <!ENTITY... >.
15XmlEntityReferenceRepresents an entity reference node.
16XmlExceptionReturns detailed information about the last exception.
17XmlImplementationDefines the context for a set of XmlDocument objects.
18XmlLinkedNodeGets the node immediately preceding or following this node.
19XmlNodeRepresents a single node in the XML document.
20XmlNodeListRepresents an ordered collection of nodes.
21XmlNodeReaderRepresents a reader that provides fast, non-cached forward only access to XML data in an XmlNode.
22XmlNotationRepresents a notation declaration, such as <!NOTATION... >.
23XmlParserContextProvides all the context information required by the XmlReader to parse an XML fragment.
24XmlProcessingInstructionRepresents a processing instruction, which XML defines to keep processor-specific information in the text of the document.
25XmlQualifiedNameRepresents an XML qualified name.
26XmlReaderRepresents a reader that provides fast, noncached, forward-only access to XML data.
27XmlReaderSettingsSpecifies a set of features to support on the XmlReader object created by the Create method.
28XmlResolverResolves external XML resources named by a Uniform Resource Identifier (URI).
29XmlSecureResolverHelps to secure another implementation of XmlResolver by wrapping the XmlResolver object and restricting the resources that the underlying XmlResolver has access to.
30XmlSignificantWhitespaceRepresents white space between markup in a mixed content node or white space within an xml:space= 'preserve' scope. This is also referred to as significant white space.
31XmlTextRepresents the text content of an element or attribute.
32XmlTextReaderRepresents a reader that provides fast, non-cached, forward-only access to XML data.
33XmlTextWriterRepresents a writer that provides a fast, non-cached, forward-only way of generating streams or files containing XML data that conforms to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations.
34XmlUrlResolverResolves external XML resources named by a Uniform Resource Identifier (URI).
35XmlWhitespaceRepresents white space in element content.
36XmlWriterRepresents a writer that provides a fast, non-cached, forward-only means of generating streams or files containing XML data.
37XmlWriterSettingsSpecifies a set of features to support on the XmlWriter object created by the XmlWriter.Create method.

ADVERTISEMENTS

Parsing XML with DOM API

S.NMethod Name & Description
1AppendChild
Adds the specified node to the end of the list of child nodes, of this node.
2CreateAttribute(String)
Creates an XmlAttribute with the specified Name.
3CreateComment
Creates an XmlComment containing the specified data.
4CreateDefaultAttribute
Creates a default attribute with the specified prefix, local name and namespace URI.
5CreateElement(String)
Creates an element with the specified name.
6CreateNode(String, String, String)
Creates an XmlNode with the specified node type, Name, and NamespaceURI.
7CreateNode(XmlNodeType, String, String)
Creates an XmlNode with the specified XmlNodeType, Name, and NamespaceURI.
8CreateNode(XmlNodeType, String, String, String)
Creates a XmlNode with the specified XmlNodeType, Prefix, Name, and NamespaceURI.
9CreateProcessingInstruction
Creates an XmlProcessingInstruction with the specified name and data.
10CreateSignificantWhitespace
Creates an XmlSignificantWhitespace node.
11CreateTextNode
Creates an XmlText with the specified text.
12CreateWhitespace
Creates an XmlWhitespace node.
13CreateXmlDeclaration
Creates an XmlDeclaration node with the specified values.
14GetElementById
Gets the XmlElement with the specified ID.
15GetElementsByTagName(String)
Returns an XmlNodeList containing a list of all descendant elements that match the specified Name.
16GetElementsByTagName(String, String)
Returns an XmlNodeList containing a list of all descendant elements that match the specified LocalName and NamespaceURI.
17InsertAfter
Inserts the specified node immediately after the specified reference node.
18InsertBefore
Inserts the specified node immediately before the specified reference node.
19Load(Stream)
Loads the XML document from the specified stream.
20Load(String)
Loads the XML document from the specified URL.
21Load(TextReader)
Loads the XML document from the specified TextReader.
22Load(XmlReader)
Loads the XML document from the specified XmlReader.
23LoadXml
Loads the XML document from the specified string.
24PrependChild
Adds the specified node to the beginning of the list of child nodes for this node.
25ReadNode
Creates an XmlNode object based on the information in the XmlReader. The reader must be positioned on a node or attribute.
26RemoveAll
Removes all the child nodes and/or attributes of the current node.
27RemoveChild
Removes specified child node.
28ReplaceChild
Replaces the child node oldChild with newChild node.
29Save(Stream)
Saves the XML document to the specified stream.
30Save(String)
Saves the XML document to the specified file.
31Save(TextWriter)
Saves the XML document to the specified TextWriter.
32Save(XmlWriter)
Saves the XML document to the specified XmlWriter.

ADVERTISEMENTS

XML Parser APIs

<?xml version="1.0"?>

<collection shelf="New Arrivals">
<movie title="Enemy Behind">
   <type>War, Thriller</type>
   <format>DVD</format>
   <year>2003</year>
   <rating>PG</rating>
   <stars>10</stars>
   <description>Talk about a US-Japan war</description>
</movie>
<movie title="Transformers">
   <type>Anime, Science Fiction</type>
   <format>DVD</format>
   <year>1989</year>
   <rating>R</rating>
   <stars>8</stars>
   <description>A schientific fiction</description>
</movie>
   <movie title="Trigun">
   <type>Anime, Action</type>
   <format>DVD</format>
   <episodes>4</episodes>
   <rating>PG</rating>
   <stars>10</stars>
   <description>Vash the Stampede!</description>
</movie>
<movie title="Ishtar">
   <type>Comedy</type>
   <format>VHS</format>
   <rating>PG</rating>
   <stars>2</stars>
   <description>Viewable boredom</description>
</movie>
</collection>

Example 1

Imports System.Xml
Public Class Form1

   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.   
      Me.Text = ""
   End Sub
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      ListBox1().Items.Clear()
      Dim xr As XmlReader = XmlReader.Create("movies.xml")
      Do While xr.Read()
          If xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "movie" Then
              ListBox1.Items.Add(xr.GetAttribute(0))
          End If
      Loop
   End Sub
   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
      ListBox2().Items.Clear()
      Dim xr As XmlReader = XmlReader.Create("movies.xml")
      Do While xr.Read()
          If xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "type" Then
              ListBox2.Items.Add(xr.ReadElementString)
          Else
              xr.Read()
          End If
      Loop
   End Sub
   Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
      ListBox3().Items.Clear()
      Dim xr As XmlReader = XmlReader.Create("movies.xml")
      Do While xr.Read()
          If xr.NodeType = XmlNodeType.Element AndAlso xr.Name = "description" Then
              ListBox3.Items.Add(xr.ReadElementString)
          Else
              xr.Read()
          End If
      Loop
   End Sub
End Class

Example 2

Imports System.Xml
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.   
      Me.Text = ""
   End Sub
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim xws As XmlWriterSettings = New XmlWriterSettings()
      xws.Indent = True
      xws.NewLineOnAttributes = True
      Dim xw As XmlWriter = XmlWriter.Create("authors.xml", xws)
      xw.WriteStartDocument()
      xw.WriteStartElement("Authors")
      xw.WriteStartElement("author")
      xw.WriteAttributeString("code", "1")
      xw.WriteElementString("fname", "Zara")
      xw.WriteElementString("lname", "Ali")
      xw.WriteEndElement()
      xw.WriteStartElement("author")
      xw.WriteAttributeString("code", "2")
      xw.WriteElementString("fname", "Priya")
      xw.WriteElementString("lname", "Sharma")
      xw.WriteEndElement()
      xw.WriteStartElement("author")
      xw.WriteAttributeString("code", "3")
      xw.WriteElementString("fname", "Anshuman")
      xw.WriteElementString("lname", "Mohan")
      xw.WriteEndElement()
      xw.WriteStartElement("author")
      xw.WriteAttributeString("code", "4")
      xw.WriteElementString("fname", "Bibhuti")
      xw.WriteElementString("lname", "Banerjee")
      xw.WriteEndElement()
      xw.WriteStartElement("author")
      xw.WriteAttributeString("code", "5")
      xw.WriteElementString("fname", "Riyan")
      xw.WriteElementString("lname", "Sengupta")
      xw.WriteEndElement()
      xw.WriteEndElement()
      xw.WriteEndDocument()
      xw.Flush()
      xw.Close()
      WebBrowser1.Url = New Uri(AppDomain.CurrentDomain.BaseDirectory + "authors.xml")
   End Sub
End Class

Example 3

Imports System.Xml
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.   
      Me.Text = ""
   End Sub
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      ListBox1.Items.Clear()
      Dim xd As XmlDocument = New XmlDocument()
      xd.Load("authors.xml")
      Dim newAuthor As XmlElement = xd.CreateElement("author")
      newAuthor.SetAttribute("code", "6")
      Dim fn As XmlElement = xd.CreateElement("fname")
      fn.InnerText = "Bikram"
      newAuthor.AppendChild(fn)
      Dim ln As XmlElement = xd.CreateElement("lname")
      ln.InnerText = "Seth"
      newAuthor.AppendChild(ln)
      xd.DocumentElement.AppendChild(newAuthor)
      Dim tr As XmlTextWriter = New XmlTextWriter("movies.xml", Nothing)
      tr.Formatting = Formatting.Indented
      xd.WriteContentTo(tr)
      tr.Close()
      Dim nl As XmlNodeList = xd.GetElementsByTagName("fname")
      For Each node As XmlNode In nl
          ListBox1.Items.Add(node.InnerText)
      Next node
   End Sub
End Class