17 Replies - 4712 Views - Last Post: 14 July 2009 - 06:27 AM
#1
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
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
Replies To: Read in XML File from Internet
#2
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
Then you have a Generic List populated with all the prices from the XML file.
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.
#3
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?
question. just for my own knowning as i like to know what it does in my code
what does doc =null; do?
#4
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));
}
#5
Re: Read in XML File from Internet
Posted 13 July 2009 - 01:07 PM
#6
#7
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.
#8
Re: Read in XML File from Internet
Posted 13 July 2009 - 01:29 PM
Shiggins, on 13 Jul, 2009 - 03:22 PM, said:
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.
#9
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?
i had to read them in as a string then convert them to float. i cant read them in as a float?
#10
Re: Read in XML File from Internet
Posted 13 July 2009 - 01:40 PM
PsychoCoder, 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.
#11
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
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.
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.
#12
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.
#13
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");
#14
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?
#15
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.
|
|

New Topic/Question
Reply




MultiQuote




|