I want to get the contact node where corresponding name is provided.
please help me I am just starting xml.
Thank you in advance.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Damodar29 reputationPosted Aug 6, 2010, 11:14 AM EDT
Please mark as answer if you are satisfied.
Damodar29 reputationPosted Aug 6, 2010, 8:38 AM EDT
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load ("File");
XmlNode node = xmldoc.DocumentElement;
foreach (XmlNode InfoNode in node.ChildNodes)
{
if (InfoNode.InnerText == "tanmay")
{
if(InfoNode.Name == "contact")
{
string contactno = InfoNode.Innertext;
// this gives you the contact no when the name is given.
}
}
}
use this .. may be this will help you.
Tanmay SarkarRank #1,407 · 1.2K reputationPosted Aug 6, 2010, 9:08 AM EDT
So i change it to string contactno = InfoNode.InnerText.ToString();
but it not showing any thing! Is i have a mistake?
My xml file is as follows.
help please..
Damodar29 reputationPosted Aug 6, 2010, 9:17 AM EDT
foreach( XmlNode phonelist in node.ChildNodes)
{
foreach (XmlNode pbdat in phonelist.ChildNodes)
{
if (pbdat.InnerText == "tanmay")
{
if(pbdat.Name == "contact")
{
string contactno = InfoNode.Innertext;
// this gives you the contact no when the name is given.
}
}
}
you told u only had one Information parent node. so i wrote only one foreach loop. Try this it should work now.
Tanmay SarkarRank #1,407 · 1.2K reputationPosted Aug 6, 2010, 9:35 AM EDT
private void button1_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("test.xml");
XmlNode node = xmldoc.DocumentElement;
foreach (XmlNode phonelist in node.ChildNodes)
{
foreach (XmlNode pbdat in phonelist.ChildNodes)
{
if (pbdat.InnerText == "Tanmay")
{
if (pbdat.Name == "contact")
{
string contactno = pbdat.InnerText;
MessageBox.Show(contactno);
}
}
}
}
}
If you have a little bit time can you please upload your cs file.
Thank you in advance.
Sam HobbsRank #107 · 20.5K reputationPosted Aug 6, 2010, 1:42 PM EDT
Tanmay SarkarRank #1,407 · 1.2K reputationPosted Aug 6, 2010, 8:21 PM EDT
like:- ..... where name 'tanmay'
there will be two text field one will provide name by user & click button & another field will show the contact. That's my basic thing. And when I starting sql it was also my basic program on that time.
Thank you! :)
Tanmay SarkarRank #1,407 · 1.2K reputationPosted Aug 6, 2010, 8:35 PM EDT
so the code will be like,
if (pdata.Name == "contact")
{
MessageBox.Show("contact no : " + pdata.InnerText);
rtnval = "N";
}
it's work fine now.
Thank you so much! :)
Sam HobbsRank #107 · 20.5K reputationPosted Aug 6, 2010, 9:23 PM EDT
There are many ways to do many things and many times the first suggestion is not always the best for you.
Tanmay SarkarRank #1,407 · 1.2K reputationPosted Aug 6, 2010, 9:52 PM EDT
you take my point what i am want perfectly ....
Thank you sir :)
Sam HobbsRank #107 · 20.5K reputationPosted Aug 6, 2010, 11:25 PM EDT
Also, if you are not familiar with the XmlSerializer Class then look at my article in this web site about the XmlSerializer Class; it does not use SQL but it is worth knowing about.
Tanmay SarkarRank #1,407 · 1.2K reputationPosted Aug 7, 2010, 2:36 AM EDT
Please put here sir your article link. specially want to learn XmlSerializer .
Thank you sir. I am waiting.....
Sam HobbsRank #107 · 20.5K reputationPosted Aug 7, 2010, 6:05 AM EDT
Tanmay SarkarRank #1,407 · 1.2K reputationPosted Aug 7, 2010, 6:41 AM EDT
Thank you sir :)