VBdotnet2005
asked on
Read XML data
I am testing a free web service. http://www.webservicex.net/ws/WSDetails.aspx?CATID=2&WSID=9. I got it to return data in xml. What is the best way of reading it?
protected void Page_Load(object sender, EventArgs e)
{
StockQuote s = new StockQuote();
string XmlStr = s.GetQuote("GOOG");
}
<StockQuotes>
- <Stock>
<Symbol>GOOG</Symbol>
<Last>888.035</Last>
<Date>9/24/2013</Date>
<Time>11:34am</Time>
<Change>+1.535</Change>
<Open>886.50</Open>
<High>890.10</High>
<Low>881.40</Low>
<Volume>652238</Volume>
<MktCap>295.7B</MktCap>
<PreviousClose>886.50</Pre viousClose >
<PercentageChange>+0.17%</ Percentage Change>
<AnnRange>636.00 - 928.00</AnnRange>
<Earns>34.565</Earns>
<P-E>25.65</P-E>
<Name>Google Inc.</Name>
</Stock>
</StockQuotes>
protected void Page_Load(object sender, EventArgs e)
{
StockQuote s = new StockQuote();
string XmlStr = s.GetQuote("GOOG");
}
<StockQuotes>
- <Stock>
<Symbol>GOOG</Symbol>
<Last>888.035</Last>
<Date>9/24/2013</Date>
<Time>11:34am</Time>
<Change>+1.535</Change>
<Open>886.50</Open>
<High>890.10</High>
<Low>881.40</Low>
<Volume>652238</Volume>
<MktCap>295.7B</MktCap>
<PreviousClose>886.50</Pre
<PercentageChange>+0.17%</
<AnnRange>636.00 - 928.00</AnnRange>
<Earns>34.565</Earns>
<P-E>25.65</P-E>
<Name>Google Inc.</Name>
</Stock>
</StockQuotes>
ASKER
Are you saying to put the returned data above into an xml file and read it that way?
You don't have to put it in a file; you can leave it in a MemoryStream if you prefer. The XmlSerializer has overloaded methods that accept Stream instances.
ASKER
I am sorry, I am very new to this. Instead of using XmlTextWriter , use XmlTextReader?
Below is your sample. Can you give me another sample
static void SerializeIt(string data, string filename)
{
XmlSerializer serializer = new XmlSerializer(data.GetType ());
using (XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8))
{
try
{
serializer.Serialize(write r, data);
Console.WriteLine("Seriali zation successful! Wrote test.dat!");
}
catch (InvalidOperationException )
{
Console.WriteLine("Failed to serialize object!!");
}
}
}
Below is your sample. Can you give me another sample
static void SerializeIt(string data, string filename)
{
XmlSerializer serializer = new XmlSerializer(data.GetType
using (XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8))
{
try
{
serializer.Serialize(write
Console.WriteLine("Seriali
}
catch (InvalidOperationException
{
Console.WriteLine("Failed to serialize object!!");
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You might consider serialization. I wrote a couple of articles on the subject:
https://www.experts-exchange.com/Programming/Languages/.NET/A_3024-Introduction-to-XML-Serialization-in-NET.html
https://www.experts-exchange.com/Programming/Languages/.NET/A_3041-Writing-a-Class-Struct-to-be-XML-Serialized-and-Creating-a-Class-from-a-Schema.html