Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2005

Tip: Looking for answers? Try searching our database.

select multiple xml fragments from xml file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin - 05 Sep 2005 10:41 GMT
Hi,

I have an xml file (see below). using C# I would like to pull out all
student names and write then to screen.
As there may be an unknown number of <studentName> tags in the file, what i
would like to do is "select" all <studentName>
nodes and for each one write out first name and last name.

can anybody point me in the direction (or show me how to do this.)

what I would like is something like the following foreach loop

foreach studentname element in the xmldocument
{
   console.writeline(...student first name)
   console.writeline(...student last name)
}

many thanks in advance.

cheers

martin.

============================================================================
=====================================================

<names>
<college>xml College</college>
<year>1995</year>
<class>orange</class>
<studentName>
 <Firstname>John</Firstname>
 <LastName>moses</LastName>
</studentName>
<studentName>
 <Firstname>greg</Firstname>
 <LastName>bennet</LastName>
</studentName>
<studentName>
 <Firstname>lucus</Firstname>
 <LastName>starsky</LastName>
</studentName>
<studentName>
 <Firstname>chuck</Firstname>
 <LastName>jennings</LastName>
</studentName>
<studentName>
 <Firstname>nick</Firstname>
 <LastName>edwards</LastName>
</studentName>
<studentName>
 <Firstname>gordon</Firstname>
 <LastName>bennet</LastName>
</studentName>
<studentName>
 <Firstname>james</Firstname>
 <LastName>galloway</LastName>
</studentName>
<studentName>
 <Firstname>paul</Firstname>
 <LastName>elley</LastName>
</studentName>
<studentName>
 <Firstname>simon</Firstname>
 <LastName>simone</LastName>
</studentName>
</names>
Abraham Luna - 11 Sep 2005 01:19 GMT
 XmlDocument xdNames = new XmlDocument();
 xdNames.Load(Server.MapPath("names.xml"));
 XmlNodeList xnlStudentNames = xdNames.GetElementsByTagName("studentName");
 ArrayList alNames = new ArrayList();
 foreach (XmlNode xnStudentName in xnlStudentNames)
 {
  XmlNodeList xnlNames = xnStudentName.ChildNodes;
  Console.WriteLine(xnlNames[0].InnerText);
  Console.WriteLine(xnlNames[1].InnerText);
 }

hope this helps

> Hi,
>
[quoted text clipped - 65 lines]
> </studentName>
> </names>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.