Releases the power of smart prompts for LINQ to XML in VB. NET.

Source: Internet
Author: User

As a loyal C # fan, VB. NET is rarely used for writing.Code. C # The meaning of several symbols can be expressed. In VB. NET, a lot of words are often needed to express the meaning (in another perspective, this may also be an advantage in the eyes of others ). Maybe the C/C ++/C # code is too much to read. Check VB again. net code, always think that the screen is full of letters, make me a little dazzled, can not find the focus of the Code where, where the core is. Haha.

In addition, I have tried to use LINQ to XML, but it does not seem as easy as I think. Therefore, when developing a function that requires a large amount of XML content, we should choose the classic Dom mode.

However, now. the misunderstanding and prejudice of net and LINQ to XML have been greatly improved. As a result, when I recently needed to operate a large number of XML documents, I found that I could only use C # to implement it, But I scratched my head. What prompted me to make such a big change? After the advertisement, let's look at it again.

------------------------------- Separation line generated for advertisement ---------------------------------

Recently, I took advantage of all the opportunities to use vs2010rc, and found that there were a lot of improvements over vs2008, and the running speed was much faster than beta2. I really hope we can quickly upgrade the development tool to vs2010. It would be better if the Framework Version can also be upgraded to 4.0.

Terminator can witness the release of the official version, and I will continue to get confused in World of Warcraft.

------------------------------- Separation line with the advertisement ---------------------------------

Welcome back after the advertisement. I believe most of my friends have already guessed the answer. Because I have already explained the three elements in this blog post in the title, "VB. net "and" LINQ to XML "have already been raised in the question, so the answer is naturally" smart prompt? Yes. It is a "smart prompt ".

As a spoiled MicrosoftProgramPersonnel, vs powerful Smart Tips: both credit and hard work, even when I interviewed some junior programmers, they were unable to survive without, I can't write a line of code (including the hello World Program on the console). Actually, I am almost the same. Leaving vs is basically just half-lived. In other words, as a practical development work, if there are some methods and tools that can improve development efficiency and quality, why should we hold notepad to show ourselves as a master? Of course, what you need to know must be remembered in any case. I am too far away to argue. Let's talk about the specific practices below, which saves the time and process of exploring in the dark.

First, let's take a look at the syntax of the xelement element that declares LINQ to XML in VB. NET. It is a rare syntax that looks more concise than C:

 
Dim accountnumber as string = "510087321" dim meternumber as string = "100003680" dim clientdetail as xelement = _ <clientdetail> <accountnumber> <% = accountnumber %> </accountnumber> <meternumber> <% = meternumber %> </meternumber> <clientid> 478 </clientid> </clientdetail>

 

So what should I do if I need to access the clientid sub-element "478" of the clientdetail element above? You can use the following syntax:

Dim clientid as string = clientdetail. <clientid>. Value

 

When I write the above Code to obtain the value of clientid, vs does not provide me with useful smart prompts. How can I get useful smart prompts? The key is that we need to (in general) vs/VB. NET provides the schema definition information of the XML document to be read and written, that is, the schema definition file suffixed with XSD, which is included in our project and imported into the code file, then, when we create an xelement, we define a good architecture. Then vs can identify and start providing the smart prompts we need. Here, we have provided a complete overview of all required steps. As some programming instructors, class is probably acceptable. However, I will give you a more detailed description. Continue with this article. The following will be more illustrated.

    • Include the architecture file in the project

First, we need to get the schema definition file for the XML document we want to read and write. Its suffix is ". XSD ", which we have defined by ourselves. For some common ones, their namespace itself is the place to get the XSD file. For example. Of course, this is not always a try (to: Terminator, it is not always effective ).

With the XSD file, we only need to add it to our project, for example, I created a "schema" folder (the file name and folder name can be set as needed), and then I put the XSD file in use and added it to the project, as shown below:

    • Introduce and use architectures in code files

First, we need to import the required schema definitions in the code file. The import method is similar to the import namespace, as shown in:

 

The namespace URI is the value specified by the "targetnamespace" attribute in the XSD file:

    • Our architecture is included when xelement is created and used.

The following code is used:

The above code gets the ratereply element obtained from the Web Service. When we get it, we also specify the schema definition (or namespace) to be followed as "http://fedex.com/ws/rate/v8.pdf ".

After the above efforts, when we continue to operate the rootelement variable, VS can provide us with accurate smart prompts:

Accurate smart prompts include more than one sub-item, less than one sub-item. Cool ......

 

Finishing touch:

Unfortunately, C # seems unable to provide such capabilities. Well, every language has its own advantages and disadvantages. It has its own advantages and disadvantages, as well as its own advantages and disadvantages. (Bad, eye-catching to the ground ......)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.