1

功能:

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 形式关闭。请帮助我将不胜感激

4

1 回答 1

1

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

public XmlNode getAccomodationspattern2(string AccomodationID)

然后返回一个:

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

return doc;
于 2013-02-13T14:20:20.430 回答