0

I am writing an application that will run as a service and check computers once an hour to see if they are up. I have a ComputerList.xml file that I will be reading from and writing to. The format of the xml file is

<computers>
  <PrimaryServers>
    <Location1 type="primary">
       <ipaddress>192.168.1.2</ipaddress>
       <isAlive>true</ipaddress>
    </Location1>
    <location2></location2>
  </PrimaryServers>
  <SecondaryServers>
     <location1 type="secondary">
        <ipaddress></ipaddress>
        etc...
     </location1>
  </SecondaryServers>
  <clients>
     etc... <type="client">
  </clients>
</computers>

Each location has a different number of computers and I'd like to store the values for use in other methods and write back a bool to the file whether or not the host is alive after it has been checked.

The idea for this application is check if the primary server is alive, if it's not it checks to see if the clients are alive, if they are down then I know the network is down between sites and do nothing, if just the server is down, then it pushes out a hosts file to the clients that are alive, redirecting traffic to the secondary server. I would use failover clustering but the LOB application on the server does not allow this.

All I'm looking for is a way to read the xml file, discern between primaries, secondaries, and clients, retrieve the values and store them in a dictionary. I've already written the code to see if it is alive or not which will return each devices IsAlive status, I just need to write this into the xml so I have persistent data in case the server is rebooted. There are not enough computers to justify a database.

Thanks in advance.

Bob R.
  • 15
  • 2
  • 2
    I see a project here, but I don't see a clear question. Where are you stuck? Why not start at step 1 - how to read an XML file? Break this down into smaller pieces - as it stands you have material here for at least three, and possibly more, questions. – J... Apr 21 '16 at 18:54
  • [How do I read and parse an XML file in C#?](http://stackoverflow.com/q/642293/327083) – J... Apr 21 '16 at 18:54
  • I'm wanting to parse the xml and capture the data in a way that I can loop through the IP addresses of the computers by location and write back the IsAlive value. – Bob R. Apr 21 '16 at 19:07
  • Yes, I can read your post. Reading comprehension isn't the problem - I'm saying this style of question is *too broad* for the Stack Overflow format. The style here is one question, one answer - not one project, one deliverable. We aren't going to do your work for you. This project has value to you, but the steps you take to achieve it can have value for many people. Take time to break this project down into separate questions on the specific places you are stuck. I'd start with learning how to read an XML file and work from there. – J... Apr 21 '16 at 19:45

2 Answers2

1

You can use the XmlDocument Class to read and save the XML document. It's in System.Xml.

Read more about it here:

https://msdn.microsoft.com/en-us/library/system.xml.xmldocument%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Poul Bak
  • 10,450
  • 5
  • 32
  • 57
1

Try something like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, Computer> dict = new Dictionary<string, Computer>();
            string xml =
                    "<Computers>" +
                      "<PrimaryServers>" +
                        "<Location1 type=\"primary\">" +
                          "<Ipaddress>192.168.1.2</Ipaddress>" +
                          "<IsAlive>true</IsAlive>" +
                        "</Location1>" +
                        "<Location2></Location2>" +
                      "</PrimaryServers>" +
                      "<SecondaryServers>" +
                        "<Location1 type=\"secondary\">" +
                          "<Ipaddress>192.168.1.3</Ipaddress>etc..." +
                        "</Location1>" +
                      "</SecondaryServers>" +
                      "<Clients>" +
                        "<Location1 type=\"secondary\">" +
                          "<Ipaddress>192.168.1.4</Ipaddress>etc..." +
                        "</Location1>" +
                      "</Clients>" +
                    "</Computers>";
            XDocument doc = XDocument.Parse(xml);
            List<XElement> computers = doc.Descendants("Computers").Elements().ToList();
            foreach (XElement computer in computers)
            {
                dict.Add((string)computer.Descendants("Ipaddress").FirstOrDefault(), new Computer()
                {
                    ip = (string)computer.Descendants("Ipaddress").FirstOrDefault(),
                    type = (string)computer.Descendants("Location1").FirstOrDefault().Attribute("type")
                });
            }
        }
    }
    public class Computer
    {
        public string ip { get; set; }
        public string location1 { get; set; }
        public string location2 { get; set; }
        public string type { get; set; }

    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20