I'm writing a program that accesses a public web service. The web service has one web method. After referencing the service and creating an instance of it i'm retrieving the data from the web method.

The problem: The data that gets retrieved is a long string of xml. What I don't know how to do is put it into a string variable and get the values in the tags or elements. My guess is along the lines of StringReader and DataTable objects but i'm unsure of how to link everything together.

any ideas?

thanks

Recommended Answers

All 4 Replies

Please tell me what string you get, and how you need it to be. Because messages (return value) from XML Webservice be in SOAP format, so I'm confused.

i get a string of tags:
<string xmlns="http://www.webserviceX.NET/"><StockQuotes><Stock><Symbol>AAPL</Symbol><Last>121.30</Last><Date>4/21/2009</Date><Time>3:03pm</Time><Change>+0.80</Change><Open>118.95</Open><High>122.14</High><Low>118.60</Low><Volume>13044984</Volume><MktCap>108.0B</MktCap><PreviousClose>120.50</PreviousClose><PercentageChange>+0.66%</PercentageChange><AnnRange>78.20 - 192.24</AnnRange><Earns>5.383</Earns><P-E>22.39</P-E><Name>Apple Inc.</Name></Stock></StockQuotes></string>

This is a public web service that can be found at http://www.webservicex.net/WS/WSDetails.aspx?CATID=2&WSID=9

now what i want to do is isolate the data within the tags, so say I want the data within <Last> (which is the current price of the stock).

I understood you, you can use XMLTextReader

static void Main(string[] args)
        {
            string xmlStr = "<string xmlns=\"http://www.webserviceX.NET/\"><StockQuotes><Stock><Symbol>AAPL</Symbol><Last>121.30</Last><Date>4/21/2009</Date><Time>3:03pm</Time><Change>+0.80</Change><Open>118.95</Open><High>122.14</High><Low>118.60</Low><Volume>13044984</Volume><MktCap>108.0B</MktCap><PreviousClose>120.50</PreviousClose><PercentageChange>+0.66%</PercentageChange><AnnRange>78.20 - 192.24</AnnRange><Earns>5.383</Earns><P-E>22.39</P-E><Name>Apple Inc.</Name></Stock></StockQuotes></string>";
            PrintElmentValue(xmlStr, "Last"); //pass any element name i.e Last, Change, Symbol
        }

        static void PrintElmentValue(string strAsXml, string elmentName)
        {
            XmlTextReader reader = new XmlTextReader(new MemoryStream(new ASCIIEncoding().GetBytes(strAsXml)));

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && reader.Name == elmentName)
                {
                    Console.WriteLine(reader.ReadInnerXml());
                }
            }
        }

perfect, i know this will work, logic makes perfect sense.
thanks a lot man!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.