在页面中显示 xml 代码
Show xml code in page
我正在尝试以 xml 格式生成 API who returns 信息。使用正确的 URL 时,可以正确生成 Xml 文档,但我无法显示它并将其发回。例如,在 http://maps.googleapis.com/ 中,如果我 select 一个地方,我会得到信息,并且我可以在浏览器中的一个漂亮的 XML 文档中看到它。
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>277 Bedford Avenue, Brooklyn, NY 11211, EE. UU.</formatted_address>
<address_component>
<long_name>277</long_name>
<short_name>277</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Bedford Avenue</long_name>
<short_name>Bedford Ave</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Williamsburg</long_name>
<short_name>Williamsburg</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality_level_1</type>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kings County</long_name>
<short_name>Kings County</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Estados Unidos</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>11211</long_name>
<short_name>11211</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>40.7142320</lat>
<lng>-73.9612889</lng>
</location>
<location_type>ROOFTOP</location_type>
<viewport>
<southwest>
<lat>40.7128830</lat>
<lng>-73.9626379</lng>
</southwest>
<northeast>
<lat>40.7155810</lat>
<lng>-73.9599399</lng>
</northeast>
</viewport>
</geometry>
<place_id>ChIJd8BlQ2BZwokRAFUEcm_qrcA</place_id>
</result>
这正是我想要做的。
以下是我创建 xml 的方式:
XmlDocument XmlDoc = new XmlDocument();
XmlDeclaration XmlDecl;
XmlDecl = XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");
XmlDoc.AppendChild(XmlDecl);
XmlElement RootElem = XmlDoc.CreateElement("cars");
XmlDoc.AppendChild(RootElem);
if (result.Count > 0)
{
XmlElement xmlCar;
XmlElement xmlElem;
XmlElement xmlSubElem;
foreach (VehiculoResult item in result)
{
if (!(int.Parse(driver_age) < 21 && item.TipoSeguro == 2))
{
string accris = CreateAccriscode(item);
xmlCar = XmlDoc.CreateElement("car");
RootElem.AppendChild(xmlCar);
xmlElem = XmlDoc.CreateElement("Acrisscode");
xmlElem.InnerText = accris;
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Description");
xmlElem.InnerText = "<![CDATA[" + item.Descripcion + "]]>";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Capacity");
xmlElem.InnerText = item.NumPlazas.ToString();
xmlCar.AppendChild(xmlElem);
DateTime fechaInicio = DateTime.Parse(pickup_date + " " + pickup_time);
DateTime fechaFin = DateTime.Parse(dropoff_date + " " + dropoff_time);
double price = UtilesReserva.ObtenerImporteReserva(TimeSpan.Parse((fechaFin - fechaInicio).ToString()),
item.PrecioHora, item.PrecioDia,
item.PrecioSemana, item.PrecioMes);
if (item.TipoSeguro == 2)
{
price += wService.CalcularPrecioSeguro(fechaInicio, fechaFin);
}
xmlElem = XmlDoc.CreateElement("Price");
xmlElem.InnerText = price.ToString();
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("IncludedDistance");
if (item.LimitacionKM == 1)
{
xmlElem.InnerText = "250";
}
else if (item.LimitacionKM == 2)
{
xmlElem.InnerText = "500";
}
else
{
xmlElem.InnerText = "0";
}
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("IncludedDistanceUnit");
xmlElem.InnerText = "K";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("IncludedDistanceType");
xmlElem.InnerText = "D";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("ExtraDistancePrice");
xmlElem.InnerText = "0.15";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("AptTax");
xmlElem.InnerText = "0";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("AppTaxIncluded");
xmlElem.InnerText = "U";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("YoungDrvFee");
xmlElem.InnerText = "0";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("YoungDrvFeeIncluded");
xmlElem.InnerText = "Y";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("FuelPolicy");
xmlElem.InnerText = "U";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Supplier");
xmlElem.InnerText = "Social Car";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Pickup");
xmlSubElem = XmlDoc.CreateElement("Address");
xmlSubElem.InnerText = "<![CDATA[" + item.Direccion + "]]>";
xmlElem.AppendChild(xmlSubElem);
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Dropoff");
xmlSubElem = XmlDoc.CreateElement("Address");
xmlSubElem.InnerText = "<![CDATA[" + item.Direccion + "]]>";
xmlElem.AppendChild(xmlSubElem);
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Image");
xmlElem.InnerText = item.UrlFotoPerfil;
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Deep_link");
xmlElem.InnerText = "<![CDATA[" + item.Direccion + "]]>";
xmlCar.AppendChild(xmlElem);
}
}
}
生成 Xml 文档后,我尝试使用以下方法:
Response.ContentType = "text/xml"; //must be 'text/xml'
Response.ContentEncoding = System.Text.Encoding.UTF8; //we'd like UTF-8
XmlDoc.Save(Response.Output);
还有以下内容:
var stringWriter = new StringWriter();
var xmlTextWriter = XmlWriter.Create(stringWriter);
XmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
string result = stringWriter.GetStringBuilder().ToString();
saveText.Text = result; //saveText is the ID of a Label
只显示我放入 InnerText 的值,但不会显示所有 xml 代码,我想让所有内容都显示出来。有人知道我遗漏了什么或做错了什么吗?
看多了终于找到了解决办法,就一行代码:
saveText.Text = Server.HtmlEncode(XmlDoc.InnerXml);
希望对大家有所帮助!
我正在尝试以 xml 格式生成 API who returns 信息。使用正确的 URL 时,可以正确生成 Xml 文档,但我无法显示它并将其发回。例如,在 http://maps.googleapis.com/ 中,如果我 select 一个地方,我会得到信息,并且我可以在浏览器中的一个漂亮的 XML 文档中看到它。
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>277 Bedford Avenue, Brooklyn, NY 11211, EE. UU.</formatted_address>
<address_component>
<long_name>277</long_name>
<short_name>277</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Bedford Avenue</long_name>
<short_name>Bedford Ave</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Williamsburg</long_name>
<short_name>Williamsburg</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality_level_1</type>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kings County</long_name>
<short_name>Kings County</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Estados Unidos</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>11211</long_name>
<short_name>11211</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>40.7142320</lat>
<lng>-73.9612889</lng>
</location>
<location_type>ROOFTOP</location_type>
<viewport>
<southwest>
<lat>40.7128830</lat>
<lng>-73.9626379</lng>
</southwest>
<northeast>
<lat>40.7155810</lat>
<lng>-73.9599399</lng>
</northeast>
</viewport>
</geometry>
<place_id>ChIJd8BlQ2BZwokRAFUEcm_qrcA</place_id>
</result>
这正是我想要做的。
以下是我创建 xml 的方式:
XmlDocument XmlDoc = new XmlDocument();
XmlDeclaration XmlDecl;
XmlDecl = XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");
XmlDoc.AppendChild(XmlDecl);
XmlElement RootElem = XmlDoc.CreateElement("cars");
XmlDoc.AppendChild(RootElem);
if (result.Count > 0)
{
XmlElement xmlCar;
XmlElement xmlElem;
XmlElement xmlSubElem;
foreach (VehiculoResult item in result)
{
if (!(int.Parse(driver_age) < 21 && item.TipoSeguro == 2))
{
string accris = CreateAccriscode(item);
xmlCar = XmlDoc.CreateElement("car");
RootElem.AppendChild(xmlCar);
xmlElem = XmlDoc.CreateElement("Acrisscode");
xmlElem.InnerText = accris;
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Description");
xmlElem.InnerText = "<![CDATA[" + item.Descripcion + "]]>";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Capacity");
xmlElem.InnerText = item.NumPlazas.ToString();
xmlCar.AppendChild(xmlElem);
DateTime fechaInicio = DateTime.Parse(pickup_date + " " + pickup_time);
DateTime fechaFin = DateTime.Parse(dropoff_date + " " + dropoff_time);
double price = UtilesReserva.ObtenerImporteReserva(TimeSpan.Parse((fechaFin - fechaInicio).ToString()),
item.PrecioHora, item.PrecioDia,
item.PrecioSemana, item.PrecioMes);
if (item.TipoSeguro == 2)
{
price += wService.CalcularPrecioSeguro(fechaInicio, fechaFin);
}
xmlElem = XmlDoc.CreateElement("Price");
xmlElem.InnerText = price.ToString();
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("IncludedDistance");
if (item.LimitacionKM == 1)
{
xmlElem.InnerText = "250";
}
else if (item.LimitacionKM == 2)
{
xmlElem.InnerText = "500";
}
else
{
xmlElem.InnerText = "0";
}
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("IncludedDistanceUnit");
xmlElem.InnerText = "K";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("IncludedDistanceType");
xmlElem.InnerText = "D";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("ExtraDistancePrice");
xmlElem.InnerText = "0.15";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("AptTax");
xmlElem.InnerText = "0";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("AppTaxIncluded");
xmlElem.InnerText = "U";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("YoungDrvFee");
xmlElem.InnerText = "0";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("YoungDrvFeeIncluded");
xmlElem.InnerText = "Y";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("FuelPolicy");
xmlElem.InnerText = "U";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Supplier");
xmlElem.InnerText = "Social Car";
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Pickup");
xmlSubElem = XmlDoc.CreateElement("Address");
xmlSubElem.InnerText = "<![CDATA[" + item.Direccion + "]]>";
xmlElem.AppendChild(xmlSubElem);
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Dropoff");
xmlSubElem = XmlDoc.CreateElement("Address");
xmlSubElem.InnerText = "<![CDATA[" + item.Direccion + "]]>";
xmlElem.AppendChild(xmlSubElem);
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Image");
xmlElem.InnerText = item.UrlFotoPerfil;
xmlCar.AppendChild(xmlElem);
xmlElem = XmlDoc.CreateElement("Deep_link");
xmlElem.InnerText = "<![CDATA[" + item.Direccion + "]]>";
xmlCar.AppendChild(xmlElem);
}
}
}
生成 Xml 文档后,我尝试使用以下方法:
Response.ContentType = "text/xml"; //must be 'text/xml'
Response.ContentEncoding = System.Text.Encoding.UTF8; //we'd like UTF-8
XmlDoc.Save(Response.Output);
还有以下内容:
var stringWriter = new StringWriter();
var xmlTextWriter = XmlWriter.Create(stringWriter);
XmlDoc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
string result = stringWriter.GetStringBuilder().ToString();
saveText.Text = result; //saveText is the ID of a Label
只显示我放入 InnerText 的值,但不会显示所有 xml 代码,我想让所有内容都显示出来。有人知道我遗漏了什么或做错了什么吗?
看多了终于找到了解决办法,就一行代码:
saveText.Text = Server.HtmlEncode(XmlDoc.InnerXml);
希望对大家有所帮助!