2014-02-14 50 views
0

我已經寫了一個.net c#web服務,cerate的plist文件,它的工作原理,但我不能讓我的ios應用程序使用它。 我的ios代碼工作,如果我鏈接到一個實際的plist文件,但不是如果鏈接到Web服務。 我的服務創建了與我一起測試該應用程序的完全相同的plist。IOS應用程序從.net Webservice獲取plist

我的數組爲null

NSString *plistpath = @"http://test.se/Service.asmx/GetCustomerList/"; 
    NSURL *url = [NSURL URLWithString:plistpath]; 
    // array = [NSArray arrayWithContentsOfURL:url]; 
    array = [[NSArray alloc] initWithContentsOfURL:url]; 

如果我改變plistpath對例如路徑「http://test.se/customers.plist」 我的數組不爲空了。

.NET代碼(我不發佈酒店的,但它們存在) [的WebMethod] 公共無效GetCustomerList(){

List<Customer> Kundlista = new List<Customer>(); 

    Kundlista.Add(new Customer("Kund 1", "Kläppavägen 1", "0340-651026")); 
    Kundlista.Add(new Customer("Kund 2", "Kläppavägen 2", "0000-651026")); 





    using (MemoryStream respStream = new MemoryStream()) 
    { 
     using (XmlWriter xmlWriter = XmlWriter.Create(respStream, new XmlWriterSettings() { Encoding = System.Text.Encoding.UTF8, Indent = true })) 
     { 
      xmlWriter.WriteStartDocument(); 
      xmlWriter.WriteDocType("plist", "-//Apple//DTD PLIST 1.0//EN","http://www.apple.com/DTDs/PropertyList-1.0.dtd",null); 
      xmlWriter.WriteStartElement("plist"); 
      xmlWriter.WriteStartAttribute("version"); 
      xmlWriter.WriteValue("1.0"); 
      xmlWriter.WriteEndAttribute(); 
      xmlWriter.WriteStartElement("array"); 
     foreach (var customer in Kundlista) 
    { 

     xmlWriter.WriteElementString("string",customer.Name); 
    } 

     xmlWriter.WriteEndElement(); // Close dict element 
     xmlWriter.WriteEndElement(); // close plist element 
     xmlWriter.WriteEndDocument(); 
     xmlWriter.Flush(); 
     xmlWriter.Close(); 
     Context.Response.ContentType = "text/xml"; 
     Context.Response.Output.Write(Encoding.UTF8.GetString(respStream.ToArray())); 
     } 
    } 
} 

我的plist

<plist version="1.0"> 
<array> 
<string>Kund 1</string> 
<string>Kund 2</string> 
</array>  
</plist> 
+0

您是否檢查過兩種情況下的數據是否相同(標題,全套密鑰)? – Wain

+0

我複製了我的服務輸出粘貼到.plist文件中 –

回答

0

你可以寫像這樣的擴展:

public class PListWriter : XmlTextWriter 
{ 
    public PListWriter(string filename, System.Text.Encoding encoding) : base(filename, encoding) { } 
    public PListWriter(Stream filename, System.Text.Encoding encoding) : base(filename, encoding) { } 

    public override void WriteDocType(string name = "", string pubid = "", string sysid = "", string subset = "") 
    { 
     this.WriteRaw("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"); 
    } 
} 

一個使用它!

例如:

using (var ms = new MemoryStream()) 
     { 
      using (var xml = new PListWriter(ms, System.Text.Encoding.UTF8)) 
      { 
       xml.Formatting = Formatting.Indented; 
       xml.WriteStartDocument(); 
       xml.WriteDocType(); 
       xml.WriteStartElement("plist"); 
       xml.WriteAttributeString("version", "1.0"); 
       xml.WriteStartElement("array"); 

       foreach (var element in customersArray) 
       { 
        xml.WriteStartElement("dict"); 
        xml.WriteElementString("key", "Name"); 
        xml.WriteElementString("string", element.Name); 
        xml.WriteElementString("key", "Code"); 
        xml.WriteElementString("string", element.Code); 
       } 

       xml.WriteEndElement(); 
       xml.WriteFullEndElement(); 

       xml.Flush(); 

       ms.Seek(0, SeekOrigin.Begin); 

       return ms; 
      } 
     } 

它完美地爲我工作。

+0

它不適用於我,我的服務不會運行它的東西與字符串name =「」,字符串pubid =「」,字符串sysid =「」,字符串subset =「 「它不喜歡=運算符。 –

+0

您可以顯示部分代碼,您可以在其中嘗試使用它 – melvas

相關問題