【发布时间】:2015-02-18 11:33:01
【问题描述】:
我正在尝试生成一个以 xml 格式返回信息的 API。使用正确的 URL 时,正在正确生成 XmlDocument,但我无法显示它并将其发回。例如,在http://maps.googleapis.com/ 中,如果我选择一个地方,我会得到信息,我可以在浏览器中以漂亮的 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 代码,我想让所有内容都显示。任何人都知道我错过了什么或做错了什么?
【问题讨论】:
标签: c# xml visual-studio-2012 xmldocument