Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

This is the default XML code i am having.

XML
<Name>
<Text>Hi </Text>
<Text>Hello </Text>
<Text>How are you </Text>
</Name>



Could you please suggest me how to select second <text> node. I mean i want to print "Hello" as output.

Thanks
venkat.
Posted
Updated 6-Jun-17 22:53pm
Comments
Rob Philpott 21-Nov-11 6:07am    
Use xquery. Where is this XML? On disk, in an XMLDocument, where?
venkatrao palepu 21-Nov-11 6:14am    
Hi Rob,

Thanks for the quick reply.

I have the XML code in XML Document. Each time Just i want to select second only. could you please give some suggestions bcoz i am new to this XML concept. that's y.

Thanks
venkat.
koolprasad2003 21-Nov-11 6:08am    
Do you want to select second node each time or numbers may vary ?
venkatrao palepu 21-Nov-11 6:10am    
HI Prasad,

Thanks for quick reply. Each time I want to select second one only .

Thanks
venkat.
Manisha Tambade 21-Nov-11 6:22am    
you can not have same tag name(as Text) more than one time in a main node(as in Name).so first u need to write xml format correct.

Hi Venkat,

As you already using c#4.0, then try this stuff with System.Xml.Linq. It's amazing....

XDocument xmldoc = XDocument.Parse("<name><text>Hi<text>Hello<text>How are you");
XElement elResult = xmldoc.Descendants("Text").Single(x => (string)x.Value == "Hello");

Smile
Prakash
 
Share this answer
 
Comments
hamid18 31-May-18 5:04am    
this is a hard coded solution. Could you please suggest a generic approach to deal with with XML file. How to extract the second child contents generally. ?
Here it is :

C#
XmlDocument xd= new XmlDocument();
xd.LoadXml(@"<name>
            <text>Hi </text>
            <text>Hello </text>
            <text>How are you </text>
            </name>");
var result = xd.SelectNodes("/Name/Text");


if ((result!=null) && (result.Count>=2))
    Console.WriteLine(result[1].InnerText);
Console.ReadKey();


Hope it helps.
 
Share this answer
 
Comments
RaisKazi 21-Nov-11 7:46am    
My 5. Reason - Index based condition makes more sense as all the nodes are having same name.
Amir Mahfoozi 21-Nov-11 8:12am    
Thank you :)
Read this, I hope it helps you understand XML

http://www.functionx.com/csharp2/xml/Lesson02d.htm[^]
 
Share this answer
 
Comments
RaisKazi 21-Nov-11 7:47am    
My 5.
Anuja Pawar Indore 21-Nov-11 7:50am    
Thanks Rais :)
First u make xml as
<?xml version="1.0" encoding="utf-8" ?>
<Name>
<Text1>Hi </Text1>
<Text2>Hello </Text2>
<Text3>How are you </Text3>
</Name>
Now add namespace - using System.Xml;
on button click write code as-
XmlTextReader reader = new XmlTextReader("C:\\Users\\Dell\\Desktop\\New folder\\XMLFile1.xml"); //this will location and name of xml
int i = 0;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
MessageBox.Show("<" + reader.Name);
//MessageBox.Show(">");
//MessageBox.Show(reader.Value);
break;
case XmlNodeType.Text: //Display the text in each element.
i++;
if(i==2) //2 because Hello is in second node.
MessageBox.Show(reader.Value); //will show Hello
break;
case XmlNodeType.EndElement: //Display the end of the element.
//Console.Write("</" + reader.Name);
//MessageBox.Show(">");
break;
}
}

By this sample u can read any type of xml and retrive any node,innernode or any text of xml.
As a beginner this a very important concept.
 
Share this answer
 

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