Idiot + C# + xml = stuck

Man of Honour
Joined
18 Oct 2002
Posts
13,262
Location
Northallerton/Harrogate
Hi,

I'm trying to do something that's (probably) very simple, except today I'm totally rubbish and getting nowhere - mind on other things.
Take this example xml
Code:
<serverconfig>
  <servername />
  <username />
  <password />
</serverconfig>
<recipients>
  <recipient>
    <emailaddress />
    <trigger />
  </recipient>
  <recipient>
    <emailaddress />
    <trigger />
  </recipient>
  <recipient>
    <emailaddress />
    <trigger />
  </recipient>
</recipients>

What I need to do and can't work out how to because i'm retarded is read in this file, then get all the bits of stuff out I need from the serverconfig section, then do a foreach (recipients) ... do something.

Can someone explain to me in words of one syllable or with large print and pictures how to do this - with code if necessary/you don't mind.

Thanks in advance :)
 
Code:
using System;
using System.Xml;

public class getXMLData 
{
  

  void Main(string [] args) 
  {
    XmlDocument newDoc = new XmlDocument();
    newDoc.Load("documentPathHere.xml");

    XmlNodeList nodeList = newDoc.SelectNodes("/serverconfig");

    foreach (XmlElement node in nodeList) 
    {
        string servername = node.GetAttribute("servername");
        string username = node.GetAttribute("username");
        string password = node.GetAttribute("password");
    }
  }
}

That's how you would do it with the initial serverconfig

There is a GetChildNodes command for retrieving the child nodes of say Recipients, but I'm not too sure how it's used, I hope I was at least some help
 
Code:
...
string servername = node.GetAttribute("servername");
string username = node.GetAttribute("username");
string password = node.GetAttribute("password");
...

Are those not child nodes rather than attributes though? I don't see any attributes in the XML document provided.
 
Argh so you're right

Beleive it or not I've actually been writing an XMLParser/Editor at work for the last couple of days in C#, just it's been so frustratingly difficult as I hate XML and find it so complicated

When I get in to work I'll post the code up
 
Back
Top Bottom