XML Literals Tips/Tricks

Published 26 October 07 12:40 PM

A couple quick tips here when using XML literals in your Visual Basic programs.

#1: I've started using XML literals everywhere I had been using multiline strings and text merging (as I explained in this post). For instance, I started using them in my ToString methods of my classes:

Public Overrides Function ToString() As String

    Return <string>

ID       : <%= Me.ID %>

Name     : <%= Me.LastName %>, <%= Me.FirstName %>

Address  : <%= Me.Address %>

         : <%= Me.City %>, <%= Me.State %> - <%= Me.Zip %>

           </string>.Value

End Function

#2: It's also sometimes handy to pass an XElement as a parameter if you need dynamic parameters, or were using your own custom parameter objects previously:

Dim id = 1

Dim name = "Beth"

DoIt(<param>

         <customer>

             <id><%= id %></id>

             <name><%= name %></name>

         </customer>

     </param>)

 

Private Sub DoIt(ByVal param As XElement)

 

    Dim customers = From customer In param...<customer> _

            Select New Customer With _

            {.ID = customer.<id>.Value, _

             .FirstName = customer.<name>.Value}

 

    For Each c In customers

        Console.WriteLine(c.ToString())

    Next

  

End Sub

Enjoy,
-B

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# BillS said on October 26, 2007 5:24 PM:

Beth,

Cool tips - I had just linked to your earlier post and that fast you've got a second post on the same topic.

# Beth Massi said on October 26, 2007 5:30 PM:

Glad you like them!

# Leaning Into Windows said on October 28, 2007 9:30 AM:

Visual Basic 3 to Visual Basic 6 had many lessons to teach us. We did not as an industry learn these

# Seid Krvavac said on October 28, 2007 7:58 PM:

Wow!

Thank you for that tip Beth, it's a really nice little feature in 9.0, I can't wait to the final release to use it in production!

And I also want to say at this point that you are doing a great job! Keep up the good work :-)

# Josh Stodola said on October 29, 2007 9:27 AM:

Thanks I am quite interested in these types of posts.  Hope your busy weekend went well.

Got a question, so what returns from the ToString method exactly?  Are those colons included?  Does it actually return an object with those properties rather than a string?  I just can't picture what gets returned there.

Thanks!  Hope you can continue to feed us very helpful examples!

# Beth Massi said on October 29, 2007 12:34 PM:

Hi Josh,

Yes the formatted string is returned. i.e.:

ID          : 1

Name     : Massi, Beth

Address  : 123 Main Street

             : Seattle, WA - 98123

I'm eating a lot of XML in VB lately so look for some more tips soon. ;-)

# Beth Massi said on October 29, 2007 12:53 PM:

Hey Josh, I just noticed that the previous comment messed up the formatting of the string when I pasted it in :-/. It would be formatted evenly of course. ;-)

# The Visual Basic Team said on November 1, 2007 3:08 PM:

We just released a new set of How-Do-I videos in our LINQ series on LINQ to XML in Visual Basic. These

# Beth Massi - Sharing the goodness that is VB said on November 2, 2007 12:02 PM:

We just released a new set of How-Do-I videos in our LINQ series on LINQ to XML in Visual Basic. These

# Andrew said on January 31, 2008 12:43 AM:

How do you include special characters? i want to use this for a SQL Query which has a WHERE Clause which checks to see if a value is <= 10.... the less than symbol screws up the xml

# Andrew said on January 31, 2008 12:51 AM:

Found a good article about the special character problem:

http://www.xml.com/pub/a/2001/01/31/qanda.html

# sian said on June 17, 2008 7:57 AM:

Hi Beth can you help me with this:

this is my xml file

<CapeQuotes>

<paper_cavg Date="13/06/2008">

<Month Name="Jun2008">180052.85</Month>

<Month Name="Jul2008">151081.99</Month>

<Month Name="Aug2008">147294.52</Month>

<Month Name="Sep2008">150905.31</Month>

<Month Name="Oct2008">152282.02</Month>

<Month Name="Nov2008">148332.85</Month>

<Month Name="Dec2008">141662.52</Month>

<Month Name="Jan2009">135553.84</Month>

<Month Name="Feb2009">131216.96</Month>

<Month Name="Mar2009">128094.25</Month>

<Month Name="Apr2009">125541.07</Month>

<Month Name="May2009">123045.44</Month>

<Month Name="Jun2009">120408.50</Month>

<Month Name="Jul2009">117480.47</Month>

<Month Name="Aug2009">113937.19</Month>

<Month Name="Sep2009">110095.17</Month>

<Month Name="Oct2009">106025.87</Month>

<Month Name="Nov2009">101857.96</Month>

<Month Name="Dec2009">97717.91</Month>

<Month Name="Jan2010">93790.46</Month>

<Month Name="Feb2010">90124.52</Month>

<Month Name="Mar2010">86659.83</Month>

<Month Name="Apr2010">83279.86</Month>

<Month Name="May2010">80105.17</Month>

<Month Name="Jun2010">77129.80</Month>

<Month Name="Jul2010">74352.18</Month>

<Month Name="Aug2010">71726.16</Month>

<Month Name="Sep2010">69332.67</Month>

<Month Name="Oct2010">67125.57</Month>

<Month Name="Nov2010">65099.11</Month>

<Month Name="Dec2010">63251.50</Month>

<Month Name="Jan2011">61603.07</Month>

<Month Name="Feb2011">60135.32</Month>

<Month Name="Mar2011">58808.34</Month>

<Month Name="Apr2011">57564.34</Month>

<Month Name="May2011">56436.15</Month>

</paper_cavg>

Then this is my xml query in VB but it doesn't work

Dim MyQuotes = XDocument.Load("C:\WebSite1\App_Data\CapeQuotes.xml")

Dim Quote2 = From quote In MyQuotes...<paper_cavg> _

                        Where quote.<paper_cavg>.@Date = "13/06/2008" _

                        Select quote Where quote.<paper_cavg>.<Month>.@Name = "Jul2008"

           aryOptions(i).NormUl = Quote2.ToString

# sian said on June 17, 2008 7:59 AM:

Of course the xml file has a closing literal as well

</CapeQuotes>

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About Beth Massi

Beth is a Program Manager on the Visual Studio Community Team at Microsoft and is responsible for producing and managing content for business application developers, driving community features and team participation onto MSDN Developer Centers (http://msdn.com), and helping make Visual Studio one of the best developer tools in the world. She also produces regular content on her blog (http://blogs.msdn.com/bethmassi), Channel 9, and a variety of other developer sites and magazines. As a community champion and a long-time member of the Microsoft developer community she also helps with the San Francisco East Bay .NET user group and is a frequent speaker at various software development events. Before Microsoft, she was a Senior Architect at a health care software product company and a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks, web and Windows-based applications using Microsoft development tools in a variety of businesses. She loves teaching, hiking, mountain biking, and driving really fast.

This Blog

Syndication

Page view tracker