< Parent > < Child >">
Welcome to Strong Coders Community Sign in | Join | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Reading an XML file

Recently, I was working on a Windows service where I had to create a custom XML configuration file and read it. So, I figured I'd share how I did it.

XML configuration file:

xml version = " 1.0 " ?>

< Parent >

  < Child >

    < Node1 > Blah1 Node1 >

    < Node2 > Blah2 Node2 >

    < Node3 > Blah3 Node3 >

  Child >

  < Child >

    < Node1 > Blah4 Node1 >

    < Node2 > Blah5 Node2 >

    < Node3 > Blah6 Node3 >

  Child >

Parent >

C# Code:

        using System.Xml;

 

        XmlDocument configurationFile = new XmlDocument ();

 

        string xmlFilePath = Server.MapPath( "~/MyFile.xml" );

        configurationFile.Load(xmlFilePath);

 

        XmlNodeList parentNodeList = configurationFile.SelectNodes( "Parent/Child" );

 

        foreach ( XmlNode childNode in parentNodeList)

        {

            string node1, node2, node3;

 

            node1 = childNode.SelectSingleNode( "Node1" ).InnerText;

            node2 = childNode.SelectSingleNode( "Node2" ).InnerText;

            node3 = childNode.SelectSingleNode( "Node3" ).InnerText;

        }

VB.NET Code:

        Dim configurationFile As New XmlDocument()

 

        Dim xmlFilePath As String = Server.MapPath( "~/MyFile.xml" )

        configurationFile.Load(xmlFilePath)

 

        Dim parentNodeList As XmlNodeList = configurationFile.SelectNodes( "Parent/Child" )

 

        For Each childNode As XmlNode In parentNodeList

            Dim node1, node2, node3 As String

 

            node1 = childNode.SelectSingleNode( "Node1" ).InnerText

            node2 = childNode.SelectSingleNode( "Node2" ).InnerText

            node3 = childNode.SelectSingleNode( "Node3" ).InnerText

        Next

Share this post: | bookmark it! | digg it! | reddit! | kick it! | live it!
Posted: Tuesday, May 09, 2006 8:08 AMby Ryan Olshan
Filed under:

Comments

FusionGuy said:

You've got typo in the start of the second child element.  Should be not
# May 9, 2006 8:32 AM

Ryan Olshan said:

Woopsie. Thanks for pointing that out. It's fixed pending clearing of CS cache.

Ryan

# May 9, 2006 8:40 AM
New Comments to this post are disabled