Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have xml document like this:

<level1>
<level2>
<level3>
<attribute1>
<attribute2>
<attribute3>
</level3>
<level3>
<attribute1>
<attribute2>
<attribute3>
</level3>
</level2>
<level2>
<level3>
<attribute1>
...
...
...



I'm using c#, and I want to go thru all "level3", and for every "level3", i want to read attribute2, and if it says "true", i want to print the corresponding attribute3 (can be "level3" without these attributes).

I keep the xml in XmlDocument.
Then I keep all the "level3" nodes like this:
XmlNodeList xnList = document.SelectNodes(String.Format("/level1/level2/level3"));

(document is the XmlDocument).

But from now on, I don't know exactly how to continue. I tried going thru xnList with for..each, but nothing works fine for me..

Thanks a lot
Posted
Updated 10-Oct-11 8:51am
v2

Try:
C#
XmlNodeList xnList = document.SelectNodes("/level1/level2/level3/*[self::attribute1 or self::attribute2 or self::attribute3]");

This should give you a list of all attribute nodes.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Oct-11 17:52pm    
It would make sense if it was XML, but it is not -- please see my answer.
--SA
user_code 11-Oct-11 2:26am    
Thanks Adnre, works fine!
No, you don't have XML "document like this". What you show in your question is not an XML code, this is something not well-formed so it won't parse at all. If you want to ask about parsing some text which has a structure resembling XML, explain this format and ask your question about it, but don't ask a question about XML — there is no such thing in your question.

—SA
 
Share this answer
 
Comments
user_code 11-Oct-11 2:25am    
This is XML. It just without spaces (but still works) and I just show the structure of the file, as you can see, the XML file is not ended. If you want to help, so help. If you can't understand the structure of the XML file I wrote, it's your own problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900