IOS App从.net Webservice获取plist

时间:2014-02-14 11:11:56

标签: c# ios .net objective-c web-services

我写了一个.net c#网络服务,它是一个plist文件,它可以工作,但我不能让我的ios应用程序使用它。 如果我链接到一个实际的plist文件,我的ios代码可以工作,但如果链接是Web服务,则不行。 我的服务创建了与我测试应用程序完全相同的plist。

我的数组为空

    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”的路径 我的数组不再为null了。

.NET代码(我不发布该属性,但它们存在)         [的WebMethod]     public void 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>

1 个答案:

答案 0 :(得分: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;
            }
        }

它非常适合我。