2012-08-07 135 views
2

我一直在使用windows phone 7應用程序,並且在從web服務中獲取數據時遇到了問題。即時通訊從服務器得到一個這樣的模式,我無法弄清楚如何獲取它的響應。請幫幫我。下面是來自服務器的響應。windows phone xml解析

此XML文件似乎沒有任何關聯的樣式信息。文檔樹如下所示。

<response xmlns="uri:abc.com" status="Ok" op="DelegateList" msg="Get Delegate List Successful"> 
<list count="19"> 
<delegateinfo photoid="" id="12936"> 
<prop value="A" name="fname" label="First Name"/> 
<prop value="A" name="lname" label="Last Name"/> 
<prop value="Karachi, PK" name="ccountry" label="Country/City"/> 
<prop value="Airlines & Aviation" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12938"> 
<prop value="aaa" name="fname" label="First Name"/> 
<prop value="aaaa" name="lname" label="Last Name"/> 
<prop value="aa" name="ptitle" label="Professional Title"/> 
<prop value="aaaa" name="company" label="Company"/> 
<prop value="aa,aa" name="ccountry" label="Country/City"/> 
<prop value="Airlines & Aviation" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12949"> 
<prop value="ad" name="fname" label="First Name"/> 
<prop value="ad" name="lname" label="Last Name"/> 
<prop value="a,b" name="ccountry" label="Country/City"/> 
<prop value="Airlines & Aviation" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12956"> 
<prop value="Asdsad" name="fname" label="First Name"/> 
<prop value="Asdasd" name="lname" label="Last Name"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Dehli, India" name="ccountry" label="Country/City"/> 
<prop value="Architecture" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12955"> 
<prop value="asd" name="fname" label="First Name"/> 
<prop value="Asdd" name="lname" label="Last Name"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Dehli, India" name="ccountry" label="Country/City"/> 
<prop value="Architecture" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12963"> 
<prop value="A" name="fname" label="First Name"/> 
<prop value="B" name="lname" label="Last Name"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Dehli, India" name="ccountry" label="Country/City"/> 
<prop value="Architecture" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12964"> 
<prop value="a" name="fname" label="First Name"/> 
<prop value="B" name="lname" label="Last Name"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Dehli, India" name="ccountry" label="Country/City"/> 
<prop value="Architecture" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12929"> 
<prop value="G" name="fname" label="First Name"/> 
<prop value="F" name="lname" label="Last Name"/> 
<prop value="Karachi, PK" name="ccountry" label="Country/City"/> 
<prop value="Arts & Music" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="1292620120724105446.jpg" id="12926"> 
<prop value="faraz" name="fname" label="First Name"/> 
<prop value="haider" name="lname" label="Last Name"/> 
<prop value="Team Leader Iphone at Cubix Labs Inc" name="ptitle" label="Professional Title"/> 
<prop value="Cubix Labs Inc." name="company" label="Company"/> 
<prop value="Karachi, Pakistan" name="ccountry" label="Country/City"/> 
<prop value="Airlines & Aviation" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="1292120120723075111.jpg" id="12921"> 
<prop value="cubix" name="fname" label="First Name"/> 
<prop value="labs" name="lname" label="Last Name"/> 
<prop value="iphone developer at Social Cubix" name="ptitle" label="Professional Title"/> 
<prop value="Social Cubix" name="company" label="Company"/> 
<prop value="test,test" name="ccountry" label="Country/City"/> 
<prop value="Airlines & Aviation" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12939"> 
<prop value="New" name="fname" label="First Name"/> 
<prop value="One" name="lname" label="Last Name"/> 
<prop value="Karachi, PK" name="ccountry" label="Country/City"/> 
<prop value="Airlines & Aviation" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12961"> 
<prop value="E" name="fname" label="First Name"/> 
<prop value="Qwe" name="lname" label="Last Name"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Dehli, India" name="ccountry" label="Country/City"/> 
<prop value="Architecture" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12957"> 
<prop value="s" name="fname" label="First Name"/> 
<prop value="S" name="lname" label="Last Name"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Dehli, India" name="ccountry" label="Country/City"/> 
<prop value="Architecture" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12965"> 
<prop value="dassd" name="fname" label="First Name"/> 
<prop value="Sadds" name="lname" label="Last Name"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Dehli, India" name="ccountry" label="Country/City"/> 
<prop value="Architecture" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12960"> 
<prop value="Asfdcasscf" name="fname" label="First Name"/> 
<prop value="Sff" name="lname" label="Last Name"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Dehli, India" name="ccountry" label="Country/City"/> 
<prop value="Architecture" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12922"> 
<prop value="Michael" name="fname" label="First Name"/> 
<prop value="Tabour" name="lname" label="Last Name"/> 
<prop value="Software Engineer" name="ptitle" label="Professional Title"/> 
<prop value="Cubix Labs" name="company" label="Company"/> 
<prop value="Maryland, United States" name="ccountry" label="Country/City"/> 
<prop value="IT Services, Internet & Telecommunications" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="1292020120723075408.jpg" id="12920"> 
<prop value="test" name="fname" label="First Name"/> 
<prop value="test" name="lname" label="Last Name"/> 
<prop value="test" name="ptitle" label="Professional Title"/> 
<prop value="test" name="company" label="Company"/> 
<prop value="test, test" name="ccountry" label="Country/City"/> 
<prop value="Airlines & Aviation" name="industry" label="Industry"/> 
</delegateinfo> 
<delegateinfo photoid="" id="12933"> 
<prop value="Test" name="fname" label="First Name"/> 
<prop value="Test1" name="lname" label="Last Name"/> 
<prop value="Coderu" name="ptitle" label="Professional Title"/> 
<prop value="Xyz pvt ltd" name="company" label="Company"/> 
<prop value="Karachi, PK" name="ccountry" label="Country/City"/> 
<prop value="Airlines & Aviation" name="industry" label="Industry"/> 
</delegateinfo> 
</list> 
</response> 

代碼我用來混帳它並沒有給我任何希望的結果:

XDocument ListArray= XDocument.Parse(e.Result); 
var quotes = from quote in ListArray.Descendants("response").Descendants("list").Descendants("delegateinfo").Descendants("prop") 
       select new delegateinfo 
    { 

     lastName = quote.Attribute("value").Value 
    }; 
       foreach (delegateinfo stockQuote in quotes) 
       { 
        MessageBox.Show (stockQuote.lastName); 
       } 
+0

爲什麼你需要那裏有風格信息?你想做什麼?你試過了什麼代碼,出了什麼問題? – 2012-08-07 08:34:43

+0

如果是WebService,爲什麼不添加WebReference並使用生成的代理類進行通信?這樣您甚至不必查看XML並使用強類型。 – 2012-08-07 08:36:07

+0

真是奇怪的結構 – 2012-08-07 08:49:50

回答

1
List<delegateinfo> propList = new List<delegateinfo>(); 
      var list = document.Descendants().ElementAt(1); 

      foreach (var tempVar in list.Descendants()) 
      { 
       delegateinfo obj = new delegateinfo(); 
       foreach (var delegateinfo_ in tempVar.Descendants()) 
       { 
        MessageBox.Show(delegateinfo_.Attribute("value").Value); 
        if (delegateinfo_.Attribute("label").Value.CompareTo("fname") == 1) 
         obj.firstName = delegateinfo_.Attribute("value").Value; 
        else 
         if (delegateinfo_.Attribute("label").Value.CompareTo("lname") == 1) 
          obj.lastName = delegateinfo_.Attribute("value").Value; 
        else 
          if (delegateinfo_.Attribute("label").Value.CompareTo("ccountry") == 1) 
           obj.country = delegateinfo_.Attribute("value").Value; 
        else 
           if (delegateinfo_.Attribute("label").Value.CompareTo("industry") == 1) 
            obj.industry = delegateinfo_.Attribute("value").Value; 


        propList.Add(obj); 




       } 



      } 
0

我這個東西鏈接可以幫助:HTTP://www.kotancode.com/2010/ 07/30/wp7-webservices-xml/

+0

我已經試過這個,但它不幫我:( – 2012-08-07 08:55:37