2013-08-07 80 views
0

我正在使用windows phone。 我需要发布到服务器,以获得从XML我收到的数据如下windows phone解析xml到字符串

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); 

       } 
     } 

我得到的调试屏幕上的输出如下

<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> 

我需要每一个元素,从这个XML提取和使用它作为字符串,int等 我不需要把它放在一个列表中 - 我需要单独的每个元素

我该如何做到这一点?

回答

1

你可以试试:

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

在一个的XDocument加载字符串。

而且

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

    } 
} 

为您解析Windows Phone上的XML。

您有更多的信息关于XElement here