Dim msg = <msg>
This is a test!
This is a test!
</msg>.Value
MessageBox.Show(msg, "XML Literals")
Dim bookList = _
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- List of books and magazines -->
<library>
<books>
<book name="The Hunger Games" author="Suzanne Collins"/>
<book name="Breaking Dawn" author="Stephenie Meyer"/>
<book name="The Last Song" author="Nicholas Sparks"/>
</books>
<magazine>
<magazineName>"MSDN Magazine"</magazineName>
<magazineName>"Code Magazine"</magazineName>
</magazine>
</library>
bookList.Save("c:\library.xml")
Dim xmlFile = XDocument.Load("c:\library.xml")
Debug.WriteLine(xmlFile...<magazineName>.Value)
This will show in the Immediate Window “MSDN Magazine” because it’s the first name that it found. We can also get the second magazine (in this case), using Lambda Expressions:
Dim xmlFile = XDocument.Load("c:\library.xml")
Debug.WriteLine(xmlFile...<magazineName>.Where(Function(f) _
f.Value = "Code Magazine").Value)
This is for “regular” node elements but if you need to show attributes, then you should use the following syntax:
Dim xmlFile = XDocument.Load("c:\library.xml")
Debug.WriteLine(xmlFile...<book>.@author)
And for the Lambda Expression to filter for a specific value (this will search on the book name and show the author name):
Dim xmlFile = XDocument.Load("c:\library.xml")
Debug.WriteLine(xmlFile...<book>.Where(Function(f) _
f.@name = "Breaking Dawn").@author)
For Each m In From element In bookList.<library>.<magazine>.<magazineName>
Debug.WriteLine(m.Value)
For Each m In From element In bookList...<magazineName>
Debug.WriteLine(m.Value)
Next
For Each book In From element In bookList...<book>
Debug.WriteLine("Book: " & book.@name.ToString)
Debug.WriteLine("Author: " & book.@author.ToString)
' Separation line
Debug.WriteLine(New String("-"c, 40))
Next
' Using LINQ to XML to filter the information
Dim bookSearch = From b In bookList...<book> _
Where b.@name.ToString.Contains("Song") _
Select b.@name, b.@author
' Show the results
For Each book In From element In bookSearch
Debug.WriteLine("Book: " & book.name)
Debug.WriteLine("Author: " & book.author)
' Separation line
Debug.WriteLine(New String("-"c, 40))
Next
Dim f As Func(Of Integer, Integer, Integer) = Function(x, y) x + y
Dim example = _
<test>
<value><%= f(125, 125).ToString() %></value >
</test>
<test>If you have another datasource, like a List(Of T), you can list all the values, using embedded expressions, to a XML file:
<value>250</value>
</test>
' Creates a list with some book names
Dim bookList As New List(Of String)
bookList.AddRange(New String() {"The Hunger Games", "Breaking Dawn", "The Last Song"})
' Creates the XML e saves it to disk
Dim newBookList1 = _
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<library>
<books>
<%= From b In bookList Select <book><%= b %></book> %>
</books>
</library>
newBookList1.Save("c:\result.xml")
This will be the result:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>Another example of using embedded expressions is using a DataTable. In this case it will create an XML file with the attributes “name” and “author”:
<library>
<books>
<book>The Hunger Games</book>
<book>Breaking Dawn</book>
<book>The Last Song</book>
</books>
</library>
' For this example is created a DataTable manually but
' could be the result of a SQL query or stored procedure
Dim dt As New DataTable("Books")
dt.Columns.Add("Book", GetType(String))
dt.Columns.Add("Author", GetType(String))
dt.Rows.Add("The Hunger Games", "Suzanne Collins")
dt.Rows.Add("Breaking Dawn", "Stephenie Meyer")
dt.Rows.Add("The Last Song", "Nicholas Sparks")
Dim ds As New DataSet
ds.Tables.Add(dt)
' Creates the XML e with two attributes: "name" and "author"
Dim newBookList2 = _
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- my book list -->
<library>
<books>
<%= From b In ds.Tables("Books") Select _
<book name=<%= b.Item("Book") %>
author=<%= b.Item("Author") %>/> %>
</books>
</library>
' Saves it to disk
newBookList2.Save("c:\library.xml")
This will be the result:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<library>
<books>
<book name=" The Hunger Games" author="Suzanne Collins" />
<book name=" Breaking Dawn" author="Stephanie Meyer" />
<book name=" The Last Song" author="Nicholas Sparks" />
</books>
</library>
Dim xmlFile = XDocument.Load("c:\library.xml")
xmlFile...<magazineName>.Value = "New Value"
xmlFile.Save("c:\library.xml")
But using this approach, only the first value that is found will be changed. If you need to change a specific value, like you normally do, you need to filter the information first to indicate what value to change:
Dim xmlFile = XDocument.Load("c:\library.xml")
Dim element = xmlFile.<library>.<books>.<book>.Where(Function(f) _
f.@name = "The Last Song")
element.@author = "Jorge Paulino"
xmlFile.Save("c:\library.xml")
This will change the name of the author for the book “The Last Song”, from “Nicholas Sparks” to “Jorge Paulino” (that was nice!). But once again, using the Descendants property saves you some code:
Dim xmlFile = XDocument.Load("c:\library.xml")
xmlFile...<book>.Where(Function(f) _
f.@name = "The Last Song").@author = "Jorge Paulino"
xmlFile.Save("c:\library.xml")
Dim xmlFile = XDocument.Load("c:\library.xml")
Dim element = New XElement("book", _
New XAttribute("name", "XML Literals"), _
New XAttribute("author", "Jorge Paulino"))
Dim parent = xmlFile...<books>.FirstOrDefault()
parent.Add(element)
xmlFile.Save("c:\library.xml")
Or, the easy way:
Dim xmlFile = XDocument.Load("c:\library.xml")
Dim element = <book name="XML Literals" author="Jorge Paulino"/>
Dim parent = xmlFile...<books>.FirstOrDefault()
parent.Add(element)
xmlFile.Save("c:\library.xml")
In this example we can change the values of the attributes dynamically, using embedded expressions like we saw before.
Dim xmlFile = XDocument.Load("c:\library.xml")
xmlFile...<magazineName>.Remove()
xmlFile.Save("c:\library.xml")
Dim xmlFile = XDocument.Load("c:\library.xml")
xmlFile...<book>.Where(Function(f) f.@author = "Suzanne Collins").Remove()
xmlFile.Save("c:\library.xml")
Dim xmlFile = XDocument.Load("http://feeds.feedburner.com/vbtuga")
Dim blogList = xmlFile...<item>.Where(Function(f) _
f.<category>.Value = "VB.NET").<title>.ToList()
For Each item As XElement In blogList
Console.WriteLine(item.Value)
Next
Console.ReadKey()
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
Commented:
Commented: