Presenter:Beth Massi
Visual Basic developers will learn how to work with XML in Visual Basic using XML literals, embedded expressions and XML axis properties to create, query and transform XML documents easily.
Click here to go to original video page
Minute 0
My name is Beth Massey and today I m going to show you how you can get started with links to XML and Visual Basic link stands for language integrated query and it allows us to write queries over things like objects databases and XML in a standard way Visual Basic provides deep support for link to XML through what s called XML literals and XML access properties these features allow you to use a familiar convenience syntax for working with XML in your Visual Basic code that XML is a new memory XML programming API specifically designed to leverage the link query framework even though you can call the link APIs directly only Visual Basic allows you to declare XML literals and directly access XML access properties sort order to understand all this let me show you what I mean will just open up Visual Studio 2008 here you have open an artist to create a very simple project was as great a console application
Minute 1
And I can demonstrate on some of the new features of Visual Basic s XML zealot click console application and is it okay if they notice a new era son is going to start typing in declare let s see my XML variable and I ll just start typing some XML notice that the editor of understands how to format the XML as I m typing and you ll notice that if I hover over this variable 18 and further this as an ex element of day so basically this is just an XML fragment if I place this declaration in the XML declaration at the top Seminole version in case you now noticed if I have a back over it
Minute 2
It s now an ex document which means it s a complete XML document also noticed that if I change the name of one of the them in the opening tags and go move off of it you ll see that the ending tag automatically changes for you to the XML is built directly into Visual Basic and he understands exactly what we re trying to express so everything that we can do with XML if in fact the Visual Basic code directly into this literal so if I have do less than percent equals this opens up what s called an embedded expression and we can go ahead and type in the Visual Basic code we want in here so let s go ahead and see what this looks like I missed a rapist to disk my computer filesystem right off tax and we ll just call it my XML s amount in XML and then
Minute 3
Folks in case I need to make sure that I send this is straight and so we can just say to straight this one really nice thing about dumb ex document and ex element classes is there to strain on the it actually comes out the contents of the XML document or ask element case that let s go ahead and run this and you ll see ago that s the value of now with the current date and time bayonet put it directly into our XML so let s do something a little more interesting so might want to do is create an XML document of all of the processes running on my machine
Minute 4
We can do this by querying over the writing process is returned by the system diagnostics process class so since they can place any Visual Basic code into a literal that means we can easily write link queries to construct the documents in this case I ll be using link to object API to clear the processes and create the document it s really really easy so let me on declaring a variable XML props to pay MS is created next element of my process processes and if I can spell very out so don t you do is open up an embedded expression here and we can write a link query so I may say from Prock in system diagnostics process get process is gained still lacked and this is where to
Minute 5
Really interesting you can mast your embedded expressions so what I can do if I can immediately go ahead and start typing out a new element of what I call this process tank and then we can just go ahead and create more elements under this and embedded more expressions and we need to get back out to RGB code case on the NSA process name came up to threads as well and will just do the count okay so that s how easy it is to construct a XML document from a link query and you re not in you not just on the strains to creating elements obviously we can easily create attributes as well so does open up another
Minute 6
Embedded expression here and often split the ID case of every created a set of elements that are processes that have been made in France and the Acuvue Nike case so let s go ahead and run this test it out so there s going to copy the same pattern here change a variable names and we don t need this and let s hit F5 and see what happens today so there you go there s an excellent document of all the processes running on my machine so that s how easy it notice that it is very intuitive for me to just build up its XML document from the top down the pace of the next thing I want to show you is how we can query over in these documents that we ve already create nothing we can use linked XML Visual Basic makes it really easy to object the right part
Minute 7
Of the XML document by what s called an XML access properties okay so there s a few different types of access properties as the descendent axis which basically allows you to grab any of the elements from anywhere you are by that name is a child axis and that just gets the next level and then there is an attribute access so let s not play with ease and what we can do is we can use this document the XML proxy we Re created in I can just say declare new variable here talks one and only when the deal is I just want to say get all of the descendents that match process K so but that s contiguous it s going to be turned basically a collection of accelerants and i e normal of ax element so to take a look at what s inside we can just go ahead and do it for each evidence
Minute 8
Of this console right mind came whatever delay line is the item gone and I m digest use the child axis now to get to me because I know where I am now here in this process and we see we have naming her this name element and I witnessed I want to write out the actual value of day of that element tantalizingly to be an object right okay so than a minute just a 10 night and I m also going to write out is the also has any acts at Acuvue axis can ID and after you throw already strings Macedonia anything else can others do a console read line gay let s see what this selected out for us case of theirs all of the names and the IDs and thanks
Minute 9
That s how easy it is to act the the elements and attributes inside the document it also right obviously link where it s okay so let s go ahead and right leg clearing out so what you do is I m going to a readily clear its dad that basically is going to get less similar results so I was safe from Cox talk in XML talks process tank would you do it on a tree anonymous type a summit select the product name value and the talk ID tank simply look will see that this is in collection are in i e memorable of anonymous type it s basically the Titans could be two strings to name any IDE okay
Minute 10
Do now as I go through each item were not we don t have an element anymore basically just already have name in 90 okay so here s ID here s name and there s IDE okay very simple so it s basically getting return the exact same things of God and run as if that now we create anonymous type instead of working with the ex elements directly right apiece thing I want to show you is I want my delete query over a document that already exists on its okay so I m not a Nazi XML document on my CD drive here it s just called my customers XML to take a look at this is just a list of customers basically from the Northwind database hate it when he do it if I won a query on your right increase over this XML case so we can do is
Minute 11
He can load that XML into an ex document object still undecided declare dim my costs equals X document load K and legislative CD my customer Scott XML okay so this gets this way you can load XML data from debts to pay so D X document and the ex element classes both have of load shared methods of okay so initially queried and I m just going to select all of the customers that her home residence of the UK in case you live in the country UK so much is declaring a variable here called UK customers pay and create calm and ask elements in case I want basically the XML with just the customers in the UK
Minute 12
Asa lets just call it UK customers canyon in the Godhead and again will open up our embedded expression and I ll readily query over the my costs contained from custody in high costs tank and I will use the descendents property tax is property to get the customer element now noticed in this piece of XML is go back and take a look at XML really quick to casein is not lowercase and tank XML is case sensitive case of this is not different than what you re used in Visual Basic or Visual Basic it has a case insensitivity but XML does not case the wind you are querying over your XML you have to make sure you use the right case thanks to a then I m going to say where cost
Minute 13
Country value is equal to UK and then I m going to just select cost okay so let s go ahead and copy this so you can see these kinds and contest as change the variables and I will comment us out and see this around and they should select just our customers in the UK
Minute 14
Check out the Visual Basic developer Center will be posting updates these videos there please also check out the Visual Basic team blog and my blog for more information if it
Sunday, May 25, 2008
Video: How Do I: Get Started with LINQ to XML?
Labels:
linq to xml
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment