473,887 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XElement questions

I have an XML and I am using XDocument to traverse the xml document. A
sample document look like the following
<root>
<node1>
<element1 name="n" type="1" />
</node1>
<node2 name='n1' type='2'>
<element2>tes t</element2>
</node2>
</root>

I would like to find out all the type attribute values for a given
name attribute. How can I do it with a simple linq? I am able to do it
with XmlDocument by writing recursion but there must be a easier way.
I tried the following but it is not giving any result
var result = from c in doc.Elements()
where ((c.Attribute(" name") != null) &&
(c.Attribute("t ype") != null) &&
(c.Attribute("n ame").Value == 'n'))
select (string)c.Attri bute("type").va lue;

When I debug, I can see only the root element get processed and none
of the other nodes get processed. The same when I applied Elements.

Could someone help?
Jun 27 '08 #1
4 2359
CSharper wrote:
var result = from c in doc.Elements()
where ((c.Attribute(" name") != null) &&
(c.Attribute("t ype") != null) &&
(c.Attribute("n ame").Value == 'n'))
select (string)c.Attri bute("type").va lue;

When I debug, I can see only the root element get processed and none
of the other nodes get processed. The same when I applied Elements.
Use
doc.Descendants ()
instead of
doc.Elements()
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
Hi CSharper,

Use doc.Descendants () instead of doc.Elements() in your example to get it
work. And, by the way, string convertion in select is redundant coz Value
already is string.

Regards, Alex
[TechBlog] http://devkids.blogspot.com

CI have an XML and I am using XDocument to traverse the xml document.
CA
Csample document look like the following
C<root>
C<node1>
C<element1 name="n" type="1" />
C</node1>
C<node2 name='n1' type='2'>
C<element2>test </element2>
C</node2>
C</root>
CI would like to find out all the type attribute values for a given
Cname attribute. How can I do it with a simple linq? I am able to do
Cit with XmlDocument by writing recursion but there must be a easier
Cway. I tried the following but it is not giving any result
C>
Cvar result = from c in doc.Elements()
Cwhere ((c.Attribute(" name") != null) &&
C(c.Attribute(" type") != null) &&
C(c.Attribute(" name").Value == 'n'))
Cselect (string)c.Attri bute("type").va lue;
CWhen I debug, I can see only the root element get processed and none
Cof the other nodes get processed. The same when I applied Elements.
C>
CCould someone help?
C>
Jun 27 '08 #3
The others have answered, but there is another option. You could change
doc.elements() to doc.DocumentEle ment.Elements() . It's just a style I like
that clarifies there is a document element to be skipped.
"CSharper" wrote:
I have an XML and I am using XDocument to traverse the xml document. A
sample document look like the following
<root>
<node1>
<element1 name="n" type="1" />
</node1>
<node2 name='n1' type='2'>
<element2>tes t</element2>
</node2>
</root>

I would like to find out all the type attribute values for a given
name attribute. How can I do it with a simple linq? I am able to do it
with XmlDocument by writing recursion but there must be a easier way.
I tried the following but it is not giving any result
var result = from c in doc.Elements()
where ((c.Attribute(" name") != null) &&
(c.Attribute("t ype") != null) &&
(c.Attribute("n ame").Value == 'n'))
select (string)c.Attri bute("type").va lue;

When I debug, I can see only the root element get processed and none
of the other nodes get processed. The same when I applied Elements.

Could someone help?
Jun 27 '08 #4
On May 13, 8:33*pm, Family Tree Mike
<FamilyTreeM... @discussions.mi crosoft.comwrot e:
The others have answered, but there is another option. *You could change
doc.elements() to doc.DocumentEle ment.Elements() . *It's just a style I like
that clarifies there is a document element to be skipped.

"CSharper" wrote:
I have an XML and I am using XDocument to traverse the xml document. A
sample document look like the following
<root>
* <node1>
* * * *<element1 name="n" type="1" />
* </node1>
* <node2 name='n1' type='2'>
* * * *<element2>test </element2>
* </node2>
</root>
I would like to find out all the type attribute values for a given
name attribute. How can I do it with a simple linq? I am able to do it
with XmlDocument by writing recursion but there must be a easier way.
I tried the following but it is not giving any result
var result = from c in doc.Elements()
* * * * * * * * *where ((c.Attribute(" name") != null) &&
(c.Attribute("t ype") != null) &&
* * * * * * * * * * * * * *(c.Attribute(" name").Value == 'n'))
* * * * * * * * *select (string)c.Attri bute("type").va lue;
When I debug, I can see only the root element get processed and none
of the other nodes get processed. The same when I applied Elements.
Could someone help?- Hide quoted text -

- Show quoted text -
Excellent thanks Guys.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4119
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for all companies between year 2000-2005 in my website http://www.geocities.com/allinterviewquestion/ So please have a look and make use of it.
0
4608
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
7232
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
4
2516
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for cross-posting. I am trying to build a "checklist", where a user can navigate to an ASP page on the intranet which shows a list of "questions" that the user can check off. I am trying to figure out how to do this so that it is scalable, but I am...
2
4136
by: Mike N. | last post by:
hello all: I am using LINQ to XML, but I think this issue is more widespread, here's an example of what I am trying to do: Dim testurl As String = "http://stats.enemyterritory.com/profile/ Hellfire.?xml=true" Dim pageContents As XElement = XElement.Load(testurl) The url in question is for a game statistics site where you can load
0
1003
by: Andy B | last post by:
I have a CheckBoxList that I want to dataBind to an XElement. I got this far: CheckBoxLisgt.DataSource = StockContract.Element("Glossary").Elements("Term"); CheckBoxList.DataBind(); It works in the sense that I get the "Term" element text for each list item. What I really want to do is to do something like this for an output:
4
3174
by: Rory Becker | last post by:
Is there a way to do something like the following... ------------------------------------------------------------- Dim x As String = SomeXElement.<Element1>.<SubElement>.@SomeAttribute.ValueOrDefault("SomeDefaultString") ------------------------------------------------------------- -- Rory
11
9877
by: Michael Bray | last post by:
I'm playing around with XElement stuff, and I've come across a difficulty. The XML document that I'm reading contains an xmlns declaration on the main node... <root xmlns="http://www.me.com"> <ANode> <BNode>Hello</BNode> <BNode>Goodbye</BNode> </ANode> </root>
0
889
by: John Meyer | last post by:
I'm going through the documentation help about using XElement and I'm finding no help with going through the elements as a recordset. here's an example of the return results I am getting through ISBNdb.com (snipped for brevity): <BookList total_results="287" page_size="10" page_number="1" shown_results="10"> − <BookData
4
5000
by: standerby | last post by:
I am writing a XML file suing XElement. How can I output a file like the following? I got an exception if if I use ':' in the element name or attribute name. Thanks, <?xml version="1.0" encoding="UTF-8"?> <epoStick out tongueolicySchema xmlns:epo="epo-policy" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </epoStick out tongueolicySchema>
0
9957
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10877
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10434
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9593
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7988
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5810
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4633
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4239
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.