bhaskar07
bhaskar07

Reputation: 115

How to parse data from web service

I am building my first app in windows phone 7 application. I am able to retrieve data from web service using add service reference method. The output that I am getting is

<NewDataSet>
    <UserDetails>
        <id>1</id>
        <about_details>
            Arvind Kejriwal (born 16 August 1968) is an Indian social activist
            and politician. Born in Haryana, Kejriwal is a graduate of the Indian
            Institute of Technology Kharagpur, where he studied Mechanical 
            Engineering. He is a former Indian Revenue Service (IRS) officer and 
            former Joint Commissioner in the Income Tax Department. He is well-known
            for his role in drafting a proposed Jan Lokpal Bill and his efforts to 
            bring and implement the Right to Information (RTI) act at grassroots 
            level.
            Kejriwal was born in Hisar, Haryana, on 16 August 1968 in a Bania family 
            to Gobind Ram Kejriwal and Gita Devi, a well-educated and financially 
            well-off couple.
        </about_details>
    </UserDetails>
</NewDataSet>

Now I want to only display the text in about_details in a textblock. Can anyone guide me how to do this and where to place the code for it.

   public about()
    {
        InitializeComponent();

        ServiceReference1.aapSoapClient client = new ServiceReference1.aapSoapClient();

        client.getarvindAboutCompleted += new EventHandler<ServiceReference1.getarvindAboutCompletedEventArgs>(client_getarvindAboutCompleted);

        client.getarvindAboutAsync();

    }



    void client_getarvindAboutCompleted(object sender, ServiceReference1.getarvindAboutCompletedEventArgs e)
    {

        textBlock1.Text = e.Result;
    }

Upvotes: 0

Views: 405

Answers (2)

David_001
David_001

Reputation: 5812

Assuming you are receiving that data as a single string, the simplest way for you to parse it would be to use the XDocument class.

An example might be:

var document = XDocument.Parse(response);
var aboutDetails = document.Element("NewDataSet")
                           .Element("UserDetails")
                           .Element("about_details")
                           .Value;

The above assumes your service response is in the response variable. You'd probably want a little error checking an so on to avoid NullReferenceExceptions getting thrown, but the above is the general idea.

As per @FunksMaName's comment, and your code above, the following should probably work:

void client_getarvindAboutCompleted(
    object sender, 
    ServiceReference1.getarvindAboutCompletedEventArgs e)
{
    var data = e.Result; 
    var xml = XElement.Parse(data); 
    var aboutDetails = xml.Element("UserDetails")
                          .Element("about_details")
                          .Value;
    textBlock1.Text = aboutDetails;
}

Note that you will need to ensure that System.Xml.Linq has been referenced by your project.

Upvotes: 2

BestR
BestR

Reputation: 679

Create a textblock from the tools. Place it in the screen view. Lets assume its name is textblock1.

When you get the text, write in the code:

textblock1.Text=yourtext

yourtext can be:

userDetails.about_details

Upvotes: 0

Related Questions