Damodar
posted
18 posts
since
Jun 21, 2010
from
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
Hey Tanmay,
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 Sarkar
posted
194 posts
since
May 28, 2010
from
India
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
It's through an exception in string contactno = InfoNode.Innertext; 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.
<?xml version="1.0"?> <phonebooklist> <pbdata> <name>Tanmay</name> <contact>1324569780</contact> </pbdata> </phonebooklist>
help please..
|
|
|
|
|
|
Damodar
posted
18 posts
since
Jun 21, 2010
from
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
you have to write two foreach loops. One to go into the phonelist and then to the pbdata.
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 Sarkar
posted
194 posts
since
May 28, 2010
from
India
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
Thanks, but it still not showing any thing.. may be it's my mistake to implement it. I write as you say...
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.
|
|
|
|
|
|
Damodar
posted
18 posts
since
Jun 21, 2010
from
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
here is the file. I think this is what you need. There has been a small looping problem in the earlier mentioned program. I corrected it now. Please see the file.
Please mark as answer if you are satisfied.
|
|
|
|
|
|
Sam Hobbs
posted
6490 posts
since
Sep 07, 2009
from
Los Angeles, California, USA
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
Do you neeed to do this one time per execution or many times per execution? If many times (many names) then there are more efficient ways.
|
|
|
|
|
Thinking is a feeling; pleasant for some and unpleasant for others.
|
|
|
|
|
|
Tanmay Sarkar
posted
194 posts
since
May 28, 2010
from
India
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
Sir I just want to learn how handle a simple search according where clause in sql. 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 Sarkar
posted
194 posts
since
May 28, 2010
from
India
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
Damodar thanks for your support. Your code is good but showing all contact number of another member of .xml , because when it is completed you can't set rtnval = "N";
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 Hobbs
posted
6490 posts
since
Sep 07, 2009
from
Los Angeles, California, USA
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
If you want to use SQL then use a database class such as DataTable; I don't see SQL being used in any of the other suggestions here.
There are many ways to do many things and many times the first suggestion is not always the best for you.
|
|
|
|
|
Thinking is a feeling; pleasant for some and unpleasant for others.
|
|
|
|
|
|
Tanmay Sarkar
posted
194 posts
since
May 28, 2010
from
India
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
yes sir you right, i need more ... first i must read few books deeply.. you take my point what i am want perfectly ....
Thank you sir :)
|
|
|
|
|
|
Sam Hobbs
posted
6490 posts
since
Sep 07, 2009
from
Los Angeles, California, USA
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
If you are not familiar with the DataTable.ReadXml Method then look at it.
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.
|
|
|
|
|
Thinking is a feeling; pleasant for some and unpleasant for others.
|
|
|
|
|
|
Tanmay Sarkar
posted
194 posts
since
May 28, 2010
from
India
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
Sir, can you put here your article link please.. i want to read it... but i am not getting it ... may be my search tag is wrong.
Please put here sir your article link. specially want to learn XmlSerializer .
Thank you sir. I am waiting.....
|
|
|
|
|
|
Sam Hobbs
posted
6490 posts
since
Sep 07, 2009
from
Los Angeles, California, USA
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
To search for articles, go to the web site home page; don't use the forum's search to search for articles. If you search for XmlSerializer from the web site's home page you will find my article.
|
|
|
|
|
Thinking is a feeling; pleasant for some and unpleasant for others.
|
|
|
|
|
|
Tanmay Sarkar
posted
194 posts
since
May 28, 2010
from
India
|
|
Re: Get corresponding node in Xml
|
|
|
|
|
|
|
|
|
|
|
Yes, i get it now...
Thank you sir :)
|
|
|
|
|
|