6 Months Free & No Setup Fees ASP.NET Hosting!
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » .NET 5.0 » Get corresponding node in Xml
       
Author Reply
Tanmay Sarkar
posted 194 posts
since May 28, 2010 
from India

Get corresponding node in Xml

  Posted on: 06 Aug 2010       
I have two child node in a parent

<information>
<name>tanmay</tanmay>
<contact>1234567890</contact>
</information>

I want to get the contact node where corresponding name is provided.
please help me I am just starting xml.

Thank you in advance.
Tanmay Sarkar
Damodar
posted  18 posts
since  Jun 21, 2010 
from 

 Re: Get corresponding node in Xml
  Posted on: 06 Aug 2010        0  
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.

Thanks,

Damodar

Tanmay Sarkar
posted  194 posts
since  May 28, 2010 
from  India

 Re: Get corresponding node in Xml
  Posted on: 06 Aug 2010        0  
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..
Tanmay Sarkar
Damodar
posted  18 posts
since  Jun 21, 2010 
from 

 Re: Get corresponding node in Xml
  Posted on: 06 Aug 2010        0  
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. 

Thanks,

Damodar

Tanmay Sarkar
posted  194 posts
since  May 28, 2010 
from  India

 Re: Get corresponding node in Xml
  Posted on: 06 Aug 2010        0  
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.
Tanmay Sarkar
Damodar
posted  18 posts
since  Jun 21, 2010 
from 

 Re: Get corresponding node in Xml
  Posted on: 06 Aug 2010   Accepted Answer     0  

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.

Thanks,

Damodar

Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Get corresponding node in Xml
  Posted on: 06 Aug 2010        0  
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
  Posted on: 07 Aug 2010        0  
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
Tanmay Sarkar
posted  194 posts
since  May 28, 2010 
from  India

 Re: Get corresponding node in Xml
  Posted on: 07 Aug 2010        0  
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! :)

Tanmay Sarkar
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Get corresponding node in Xml
  Posted on: 07 Aug 2010        1  
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
  Posted on: 07 Aug 2010        0  
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 :)
Tanmay Sarkar
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Get corresponding node in Xml
  Posted on: 07 Aug 2010        0  
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
  Posted on: 07 Aug 2010        0  
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.....
 
Tanmay Sarkar
Sam Hobbs
posted  6490 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: Get corresponding node in Xml
  Posted on: 07 Aug 2010        0  
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
  Posted on: 07 Aug 2010        0  
Yes, i get it now...

Thank you sir :)
Tanmay Sarkar
       
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved