----- UPDATED -----
I have a question, simple for the more experts in c# (I am a Java dissident) and I will apreciate your help to do a good thing here using the right technique and technologies.
What I need
Load a XML file from a URL. After this I need navigate between the information sets getting the data and put it into some variables.
This will be a .NET C# executable program , stand alone that other program in COBOL will run and recuperate the data.
The exemple XML is:
<?xml version="1.0" encoding="utf-8"?>
<movie>
<title>
Fast Five
</title>
<year>
2011
</year>
<description>
Dominic Toretto and his crew of street racers plan a massive heist ...
</description>
<director>
<director_1>Justin Lin</director_1>
<director_2>Vovó Mafalda</director_2>
</director>
</movie>
Accessible in, localhost:8080\movie.xml, for example.
I have a code to load a XML:
namespace main
{
public class XMLLoader
{
public static void getXMLContent()
{
String title = null;
String year = null;
String description = null;
String director1 = null;
String director2 = null;
String director3 = null;
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("http://localhost:8081/xml/movies.xml");
title = (xmlDoc.GetElementsByTagName("title")[0]).InnerText.Trim();
year = (xmlDoc.GetElementsByTagName("year")[0]).InnerText.Trim();
description = (xmlDoc.GetElementsByTagName("description")[0]).InnerText.Trim();
director1 = (xmlDoc.GetElementsByTagName("director_1")[0]).InnerText.Trim();
director2 = (xmlDoc.GetElementsByTagName("director_2")[0]).InnerText.Trim();
director3 = (xmlDoc.GetElementsByTagName("director_3")[0]).InnerText.Trim();
Console.WriteLine("Movie......: " + title);
Console.WriteLine("Year.......: " + year);
Console.WriteLine("Description: " + description);
Console.WriteLine("Director1..: " + director1);
Console.WriteLine("Director2..: " + director2);
Console.WriteLine("Director3..: " + director3);
}
catch (Exception e)
{
Console.WriteLine(" Error to load URL");
Console.WriteLine(" Message: " + e.Message);
}
}
static void Main(string[] args)
{
getXMLContent();
Console.ReadKey();
}
}
}
But this smells bad for me.
There is a more efficient and beautiful way to do it? And how to load a XML from a URL in a console C# program?
Thanks for all help and attention.
XDocumentobject, and do some research on LINQ to XML.