从WCF REST服务返回XML列表

时间:2011-04-13 12:23:42

标签: xml wcf rest

我有一个WCF Rest服务,我目前正在返回一个类型为“Book”的列表。服务正常,返回的信息不是xml格式。这使得无法解析,如何让它以xml格式显示。

或者我可以使用客户端中的类类型进行解析吗?

感谢您的帮助

服务

[WebGet(UriTemplate = "ListAllBooks", 
            ResponseFormat=WebMessageFormat.Xml,
            BodyStyle= WebMessageBodyStyle.Wrapped)]
    List<Book> ListAllBooks()
    {
        List<Book> bookList = new List<Book>();
        Book aBook;
        String query = "SELECT ID, ISBN, Title, Author, Genre, Format, Inventory, Price FROM books";
        SqlDataReader reader = queryDB(query, connectionString);
        while (reader.Read())
        {
            aBook = new Book();
            aBook.Author = reader["Author"].ToString();
            aBook.Title = reader["Title"].ToString();
            aBook.ID = reader["ID"].ToString();
            aBook.Inventory = Convert.ToInt32(reader["Inventory"]);
            aBook.ISBN = reader["ISBN"].ToString();
            aBook.Price = Convert.ToDouble(reader["Price"]);
            aBook.Genre = reader["Genre"].ToString();
            aBook.Format = reader["Format"].ToString();
            bookList.Add(aBook);
        }
        return bookList;
    }

类型手册

    public class Book
    {
        public String ID { get; set; }
        public String ISBN { get; set; }
        public String Title { get; set; }
        public String Author { get; set; }
        public String Genre { get; set; }
        public String Format { get; set; }
        public int Inventory { get; set; }
        public double Price { get; set; }
    }

0 个答案:

没有答案