Sharing the goodness...
Beth Massi is a Senior Program Manager on the Microsoft Visual Studio BizApps team who build the Visual Studio tools for Azure, Office, SharePoint as well as Visual Studio LightSwitch. Learn more about Beth.
More videos »
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
Beth,
Cool tips - I had just linked to your earlier post and that fast you've got a second post on the same topic.
Glad you like them!
Visual Basic 3 to Visual Basic 6 had many lessons to teach us. We did not as an industry learn these
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 :-)
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!
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. ;-)
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. ;-)
We just released a new set of How-Do-I videos in our LINQ series on LINQ to XML in Visual Basic. These
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
Found a good article about the special character problem:
http://www.xml.com/pub/a/2001/01/31/qanda.html
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
Of course the xml file has a closing literal as well
</CapeQuotes>