Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I would like to know about the reading of XML data returned by the services. The problem is, I have implemented the services and the services are returning the data in XML format.

I want to read that XML and fetch the data.

First Service: Returning Single Value Record

<response><status>1</status><activated>1</activated></response>

Need to read Status and Activated

Second Service: Returning Multiple Value Records

<response>
<status>1</status>
<request_id>185</request_id>
<timestamp>
<![CDATA[ 2014-03-27 02:46:07 ]]>
</timestamp>
<ads>
<ad>
<campaign_id>1</campaign_id>
<ad_id>1</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>3</campaign_id>
<ad_id>8</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>2</campaign_id>
<ad_id>14</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>11</campaign_id>
<ad_id>16</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>12</campaign_id>
<ad_id>18</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
<ad>
<campaign_id>13</campaign_id>
<ad_id>20</ad_id>
<ad_type/>
<ad_size/>
<ad_duration/>
</ad>
</ads>
</response>

I need to read ADs and Campaign id values


How can I read this during runtime.

Please provide a code.

Thanks
Posted

Learn to do it yourself: c-tutorial-reading-and-writing-xml-files[^]
 
Share this answer
 
1. You could read all the data with like xmlreader.
I don't have experience with that. but maybe this link will help you
XmlReader example[^]

2. you could use Object serialize and deserialize

first object would look like
C#
public class response
{
    public int status { get; set; }
    public int activated { get; set; }

    public response() { }
}

and then you could do
C#
XmlSerializer mySerializer = new XmlSerializer(typeof(response));
            using (FileStream stream = new FileStream(filepath, FileMode.Open))
            {
                return (response)mySerializer.Deserialize(stream);
            }




3. Linq to sql
Example using linq to sql[^]

4. XmlDocument
ReadingXml Using XmlDocument[^]

probably there are even more options
 
Share this answer
 
v2
Comments
[no name] 27-Mar-14 5:03am    
Yes, This is a good solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900