Read in XML File from Internet

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 6524 Views - Last Post: 14 July 2009 - 06:27 AM Rate Topic: -----

#1 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Read in XML File from Internet

Posted 13 July 2009 - 12:48 PM

Hi i'm making a program that i want to read from a online xml file

http://api.eve-central.com/api/evemon

pretty much i want to place the prices in an Array.
The names i dont need just the prices in an array so i can sort them out and display them in text boxes

ive looked at some examples and only be able to read the xml files. i dont know how to acutaly save the selected field i want.

any help or just a link to explain how to do it would be nice.

Regards

Rich

Is This A Good Question/Topic? 0
  • +

Replies To: Read in XML File from Internet

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Read in XML File from Internet

Posted 13 July 2009 - 12:59 PM

If you're using .Net 3.5 you should check into using LINQ To XML. It's a very powerful and easy to work with XML. Here's a small example

public System.Collections.Generic.List<string> getPriceList()
{
	  XDocument doc = Xdocument.Load("http://api.eve-central.com/api/evemon");
	  System.Collections.Generic.List<string> priceList= new System.Collections.Generic.List<string>();
	  var prices = from str in doc.Elements("minerals").Elements("mineral")
				   select (string)str.Element("price");

	  foreach (var price in prices)
		  priceList.Add(prices);

	  doc = null;
	  return priceList;
}



Then you have a Generic List populated with all the prices from the XML file.
Was This Post Helpful? 1
  • +
  • -

#3 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:04 PM

nice thanks for that it perfect..

question. just for my own knowning as i like to know what it does in my code

what does doc =null; do?
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:06 PM

I'll post my solution too, though a bit late. But maybe someone can still get something from it:

			List<double> prices = new List<double>();

			XmlDocument doc = new XmlDocument();
			WebClient webClient = new WebClient();
			Stream stream = new System.IO.MemoryStream(webClient.DownloadData("http://api.eve-central.com/api/evemon"));
			doc.Load(stream);
			XmlNodeList nodes = doc.GetElementsByTagName("price");

			foreach(XmlNode node in nodes) {
				prices.Add(double.Parse(node.InnerText, System.Globalization.CultureInfo.InvariantCulture));
			}


Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:07 PM

View PostShiggins, on 13 Jul, 2009 - 03:04 PM, said:

nice thanks for that it perfect..

question. just for my own knowning as i like to know what it does in my code

what does doc =null; do?


nothing really.
Was This Post Helpful? 0
  • +
  • -

#6 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:22 PM

View Posteclipsed4utoo, on 13 Jul, 2009 - 12:07 PM, said:

View PostShiggins, on 13 Jul, 2009 - 03:04 PM, said:

nice thanks for that it perfect..

question. just for my own knowning as i like to know what it does in my code

what does doc =null; do?


nothing really.


ok then my question then leads to why is it needed? lol and ive changed it from string to float? thats ok right? wont be any different?
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:27 PM

You'll need to convert what you select to a float. As far as doc = null;, it frees up the object (XDocument) that was created when opening the XML file for reading.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:29 PM

View PostShiggins, on 13 Jul, 2009 - 03:22 PM, said:

View Posteclipsed4utoo, on 13 Jul, 2009 - 12:07 PM, said:

View PostShiggins, on 13 Jul, 2009 - 03:04 PM, said:

nice thanks for that it perfect..

question. just for my own knowning as i like to know what it does in my code

what does doc =null; do?


nothing really.


ok then my question then leads to why is it needed? lol and ive changed it from string to float? thats ok right? wont be any different?


Most of the time, "null" is used as a piece-of-mind knowing that the object doesn't have any other references, so that the GC doesn't possibly skip it. There are very,very,very few situations where it is required.
Was This Post Helpful? 0
  • +
  • -

#9 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:31 PM

cheers got you last question then sorry to bug you all..

i had to read them in as a string then convert them to float. i cant read them in as a float?
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:40 PM

View PostPsychoCoder, on 13 Jul, 2009 - 03:27 PM, said:

You'll need to convert what you select to a float. As far as doc = null;, it frees up the object (XDocument) that was created when opening the XML file for reading.


null does not free up the Xdocument.

put this code into a Console application and run it.

static void Main(string[] args)
{
	long memoryStart = GC.GetTotalMemory(false);

	Console.WriteLine("Memory Usage At Start: {0}", memoryStart);

	XDocument doc = Xdocument.Load("http://api.eve-central.com/api/evemon");

	long memoryAfterDocLoad = GC.GetTotalMemory(false);

	Console.WriteLine("Memory Usage After Document Load: {0}", memoryAfterDocLoad);

	doc = null;

	long memoryAfterNull = GC.GetTotalMemory(false);

	Console.WriteLine("Memory Usage After Null: {0}", memoryAfterNull);

	GC.Collect();
	long memoryAfterCollection = GC.GetTotalMemory(false);

	Console.WriteLine("Memory Usage After GC Collection: {0}", memoryAfterCollection);

	Console.Read();

}



output is...

Memory Usage At Start: 783788
Memory Usage After Document Load: 1084952
Memory Usage After Null: 1084952
Memory Usage After GC Collection: 326608



If you notice, even after setting the XDocument object to null, the application still consumes the same amount of memory. Memory doesn't change until the GC runs.
Was This Post Helpful? 0
  • +
  • -

#11 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:50 PM

Hmmmm small question using this code ive got a small problem

		private void eveCentralOnlineFeedToolStripMenuItem_Click(object sender, EventArgs e)
		{
			
			EveCentral Eve = new EveCentral();;
			List<string> priceList = Eve.getPriceList();
	 
	
		}



	class EveCentral
	{

		public List<string> getPriceList()
		{
			XDocument doc = Xdocument.Load("http://api.eve-central.com/api/evemon");
			List<string> priceList = new List<string>();
			var prices = from str in doc.Elements("minerals").Elements("mineral")
						 select (string)str.Element("price");

			foreach (var price in prices)
				priceList.Add(price);

			doc = null;
			return priceList;
		}

	}




the problem im having is that it hanging like 15 seconds to get the results. where another program which is open source prases it instanly pretty much. hmmmm i cant get my head around it.
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:55 PM

that code works fine for me. takes it less than 2 seconds to return the list from the method.
Was This Post Helpful? 0
  • +
  • -

#13 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 01:56 PM

just to add it i did a breakpoint and it laggin on

XDocument doc = Xdocument.Load("http://api.eve-central.com/api/evemon");

Was This Post Helpful? 0
  • +
  • -

#14 Shiggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 09-December 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 04:29 PM

i thought it might of just been on debug but it still happening should i post a small release of the file that im testing it on to see if it happens to anyone else?
Was This Post Helpful? 0
  • +
  • -

#15 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Read in XML File from Internet

Posted 13 July 2009 - 06:44 PM

we are connecting to the same website. I don't know why it would be that much slower for you when using .Net vs. an open-source.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2