kshitijgandhi
kshitijgandhi

Reputation: 1532

windows phone parse xml to string

I am working with windows phone. I need to get data from xml i receive by posting to a server as follows

try
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response;
            response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);

            Stream streamResponse = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(streamResponse);
            var Response = streamReader.ReadToEnd();
            streamResponse.Close();
            streamReader.Close();
            response.Close();
            if (Response == "")
            {
                //show some error msg to the user        


            }
            else
            {
                //Your response will be available in "Response" 
                string mystring = Response.ToString();
                //Mytext.Text = mystring;
                Debug.WriteLine(mystring);


                //TRY

                XDocument xd = XDocument.Parse(mystring);

                Debug.WriteLine(xd);

                }
        }

I get the output on debug screen as follows

<User><Number>00000</Number><Id>1234</Id><TextKey>1A1A1A1A1A1A1A1A</TextKey><Agent>WindowsPhone</Agent></User>
<User>
  <Number>00000</Number>
  <Id>1234</Id>
  <TextKey>1A1A1A1A1A1A1A1A</TextKey>
  <Agent>WindowsPhone</Agent>
</User>

I need to extract each element from this xml and use it as strings, int etc. i don't need to put it in a list - I need each element individually

How can I achieve this?

Upvotes: 0

Views: 420

Answers (1)

Doc Roms
Doc Roms

Reputation: 3308

You can try :

 XDocument xd = XDocument.Load(XmlReader.Create(new StringReader(mystring)));

for load your string in a XDocument.

And

XElement root = xd.Root;
foreach (XElement el in root.Descendants())
{
    if (el.Name == "User")
    {

    }
}

for parse your XML on Windows Phone.

You have more information about XElement here !

Upvotes: 1

Related Questions