LINQ to XML: descendants Function

Source: Internet
Author: User

LINQ to XML: The Descendants function is in the Framework Structure of LINQ to XML. To obtain the specified sub-element under an element, call descendants extension method, as shown below:
Foreach (xelement ELEM in Doc. elements ("customers "). descendants ("customer") console. writeline (string. format ("Customer ID: {0}, name: {1}, address: {2}", ELEM. attribute ("ID "). value, ELEM. attribute ("name "). value, ELEM. attribute ("Address "). value ));
The XML used in this example is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Customers> <customer ID = "a0001" name = "Tom" address = "taipen"/> <customer ID = "a0002" name = "Mary" address = "LD "/> <customer ID = "a0003" name = "Jeff" address = "yw"/> </customers>
However, this is a special case. When XML contains a namespace, the specified element name of descendants must contain the namespace. For example, the following. dbml (definition Definition of LINQ to SQL ).
<? Xmlversion = "1.0" encoding = "UTF-8"?> <Databasename = "northwind" class = "dataclasses1datacontext" xmlns = "http://schemas.microsoft.com/linqtosql/dbml/2007"> <connectionmode = "etettings" connectionstring = "Data Source = jeffray; initial catalog = northwind; integrated Security = true "settingsobjectname =" ltsync. properties. settings "settingspropertyname =" northwindconnectionstring "provider =" system. data. sqlclient "/> ........... </database>
If the following program is used to list the persistent connection element, the receiving site is lost.
VaR CSTR = (From S1 in Doc. descendants ("connection") Select S1). First ();
The problem is due. dbml defines the namespace. When we call descendants, only the localname part of the element is imported. Check the descendants statement, you will find that what you actually receive is an object of the xname type, because the xname is actually a runtime operator containing the operator, therefore, it is natural to include the character string we entered as xname, but the xname object obtained at this time only contains the localname part and does not contain the namespace part. To retrieve the connection element, we can use the program below to compile it.
VaR CSTR = (From S1 in Doc. descendants () Where s1.name. localname = "connection" select S1). First ();
In this example, we use the descendants function without a complete number to obtain all the sub-elements, and compare them to their localname one by one, ignoring the namespace. Another method is to directly add localname using the namespace of the root element, as shown below:
VaR CSTR = (From S1 in Doc. descendants (Doc. Root. Name. namespace + "connection") Select S1). First ();
The disadvantage of this method is that the namespace of the element to be obtained must be the same as the root element.

 

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.