6.0 LINQ & XML

Source: Internet
Author: User

To is honest, after I finished the teacher ' s lecture, I didn ' t quite understand the ' topics maybe D of teaching or something else). So I searched for some simple corresponding codes to get further understanding.

Following is something that I tried.



The first part was the definition of the Customer, with three class member variables FirstName, L Astname, EmailAddress, and an overrided member function ToString () to show the whole info of a Customer.

The second part was about the function createcustomerlist (), which constructs a list of customers with initial Val UEs. As we can see, there is five customers with different names and email address seperately.

The parts above is in fact preparation for LINQ implementation. And this last part in Main () shows how LINQ works.

After creating a list of customers, here comes LINQ. Those several lines mean to select the customer from the list whose FirstName is "Donna" and th En to store the record into the result.

Then, make a subtle change to the content of the list, so, there be, customers whose FirstName is "Donna". And the result of the selection is changed automatically along with the change of the list, as we can verify in the follow ing output.


This time, I still used the first II parts of codes above to define a class of a customer and a list of customers. Then I made a change in Main () function.

First, create an xmlDocument and add an xmlElement as the rootelement of the document.

Then, in the foreach loop, create a customerelement for each customer of the list. To hold their info, we had another three xmlelements appended to the customerelement. To fill the content of each element, we can use InnerText. At the end of the loop, it's necessary to append each customerelement to the root.

As for the output, to see the structure of the xmlDocument more clearly, I put it into a. txt file, and then post it as F Ollowing.

<Customers> <Customer> <FirstName>Orlando</FirstName> <lastname>gee</las        tname> <EmailAddress>[email protected]</EmailAddress> </Customer> <Customer> <FirstName>Keith</FirstName> <LastName>Harris</LastName> <EmailAddress> [email protected]</emailaddress> </Customer> <Customer> <firstname>donna</fir Stname> <LastName>Carreras</LastName> <emailaddress>[email protected]</emailadd ress> </Customer> <Customer> <FirstName>Janet</FirstName> <lastname>g ates</lastname> <EmailAddress>[email protected]</EmailAddress> </Customer> <c ustomer> <FirstName>Lucy</FirstName> <LastName>Harrington</LastName> <e Mailaddress>[email&nbSp;protected]</emailaddress> </Customer></Customers> Please press any key to continue ...

So,,, that's all.

--------------------END &to Be continued-----------------------

6.0 LINQ & XML

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.