XML格式在将数据作为字符串返回时不会进入wcf

时间:2013-02-13 13:38:46

标签: asp.net xml wcf xslt

功能:

public string getAccomodationspattern2(string AccomodationID)
        {
            SqlCommand cmd = new SqlCommand("XML_AccommodationsDetail_V4", con);

            cmd.Parameters.AddWithValue("@AccommodationID", AccomodationID);

            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.Fill(dt);
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    Response += dt.Rows[j][k].ToString();
                }
            }

            //StringBuilder output = new StringBuilder();
            //XmlReader rawData  = XmlReader.Create(new StringReader(Response));

            //XmlWriterSettings writerSettings = new XmlWriterSettings();
            //writerSettings.OmitXmlDeclaration = true;

            //using (XmlWriter transformedData = XmlWriter.Create(output, writerSettings))
            //{
            //    XslCompiledTransform transform = new XslCompiledTransform();
            //    transform.Load(HttpContext.Current.Server.MapPath("getaccommodation2.xslt"));
            //    transform.Transform(rawData, transformedData);
            //    return output.ToString();
            //}

            XslCompiledTransform transform = new XslCompiledTransform();

            transform.Load(HttpContext.Current.Server.MapPath("getaccommodation2.xslt"));

            StringWriter results = new StringWriter();

            using (XmlReader reader = XmlReader.Create(new StringReader(Response)))
            {
                transform.Transform(reader, null, results);
            }
            return results.ToString();
            //XDocument xdoc = new XDocument(results.ToString());
            //return xdoc.Root;

        }

呼叫功能:

 [OperationContract]
    [WebGet(UriTemplate = "/AccommodationsBookingPattern/getAccommodations?id={AccomodationID}",
    BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    string getAccomodationspattern2(string AccomodationID);

问题是我收到这样的输出:

我现在得到的XML文件输出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<result><Address></Address><checkin><from>00:30</from><to>01:15</to></checkin><city>London</city><city_id>1</city_id><Location><Latitude>13131313</Latitude><Longitude>11131131</Longitude></Location><city_id>1</city_id><max_persons_in_reservation>1000</max_persons_in_reservation><name>Tahir Hotel</name><ranking>1</ranking><url>123Testing</url><zip>W2 6DX</zip></result><result><Address></Address><checkin><from>00:30</from><to>01:15</to></checkin><city>London</city><city_id>1</city_id><Location><Latitude>13131313</Latitude><Longitude>11131131</Longitude></Location><city_id>1</city_id><max_persons_in_reservation>1000</max_persons_in_reservation><name>Tahir Hotel</name><ranking>1</ranking><url>123Testing</url><zip>W2 6DX</zip></result><result>

问题

我想要正确的标签我可以打开结果地址标签并以xml格式关闭。请帮助我将感激不尽

1 个答案:

答案 0 :(得分:1)

如果您希望XML返回为XML而不是字符串,则应将返回类型更改为XmlNode

public XmlNode getAccomodationspattern2(string AccomodationID)

然后返回一个:

XmlDocument doc = new XmlDocument();
doc.LoadXml(results.ToString());

return doc;