I am trying to generate an API who returns info in xml format. When using the proper URL the XmlDocument is being generated properly but I have problems to show it and send it back. For example in http://maps.googleapis.com/ if I select a place I get the info and I can see it in the browser in a nice XML document.
<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>
This is exactly what I want to make.
Here is how I created the 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);
}
}
}
I tried using the following once I generate the Xml document:
Response.ContentType = "text/xml"; //must be 'text/xml'
Response.ContentEncoding = System.Text.Encoding.UTF8; //we'd like UTF-8
XmlDoc.Save(Response.Output);
And also the following:
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
Only the value that I put in InnerText appears but not all the xml code and I want to make appear everything. Anyone knows what I am missing or doing wrong?
After looking more and more I found the solution, it was just one line of code:
saveText.Text = Server.HtmlEncode(XmlDoc.InnerXml);
Hope it will help to others!