http://maps.googleapis.com/maps/api/geocode/xml?latlng=39.952853,32.901470&sensor=false
In this xml url, I couldn't get data from area which is explained in the below.
<long_name>Altınevler Mahallesi</long_name>
I'm using asp.net c#. Could you help me with this?
This is the code I tried to get the data from xml
XmlDocument xdoc = new XmlDocument();
xdoc.Load(
"http://maps.googleapis.com/maps/api/geocode/xml?latlng=39.952853,32.901470&sensor=false"
);
XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("entry");
foreach (XmlNode xNode in xNodelst)
{
label1.Text += "read";
}
I solved my question if anyone wants know be my guess. Solution indicated in the below.
Thank you
XmlDocument xDoc = new XmlDocument();
xDoc.Load("https://maps.googleapis.com/maps/api/geocode/xml?latlng=" +coordinate+"&location_type=ROOFTOP&result_type=street_address&key=YOURAPIKEY");
XmlNodeList xNodelst = xDoc.GetElementsByTagName("result");
XmlNode xNode = xNodelst.Item(0);
string adress = xNode.SelectSingleNode("formatted_address").InnerText;
string mahalle = xNode.SelectSingleNode("address_component[3]/long_name").InnerText;
string ilce = xNode.SelectSingleNode("address_component[4]/long_name").InnerText;
string il = xNode.SelectSingleNode("address_component[5]/long_name").InnerText;
so you can pull anydata for google maps.
Related
I am working on this since yesterday. I have an XML file which looks something like this
<catalog>
<captureInfo>
<row>5</row>
<col>5</col>
</captureInfo>
<patientInfo>
<name>XYZ</name>
<detail>details here</detail>
</patientInfo>
<imageData>
<r0c0>
<contrastFlag>true</contrastFlag>
</r0c0>
<r0c1>
<contrastFlag>true</contrastFlag>
</r0c1>
</imageData>
</catalog>
I need to update the value of contrastFlag in the XML file. This is the code I have written:
XmlDocument doc = new XmlDocument();
XmlNodeList imageData = doc.GetElementsByTagName("imageData");
foreach(XmlNode node in imageData)
{
foreach (XmlNode innernode in node)
{
if (innernode.Name == "r0c0")
{
innernode.InnerText = "false";
}
}
}
doc.Save("XMLFile1.xml");
Can anyone tell me where am I going wrong and also is there any better/faster approach for this?
Well first off, your XML is malformed, the closing should match "catalog". Why not just do this:
string xml = #"<catalog>
<captureInfo>
<row>5</row>
<col>5</col>
</captureInfo>
<patientInfo>
<name>XYZ</name>
<detail>details here</detail>
</patientInfo>
<imageData>
<r0c0>
<contrastFlag>true</contrastFlag>
</r0c0>
<r0c1>
<contrastFlag>true</contrastFlag>
</r0c1>
</imageData>
</catalog>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
xdoc.SelectSingleNode("//catalog/imageData/r0c0/contrastFlag").InnerText = "false";
Here is a way to replace all of the instances using LINQ. I just wrote out to a new file to preserve the source.
StreamReader stream = new StreamReader(#"c:\test.xml");
XDocument doc = XDocument.Load(stream);
IEnumerable<XElement> flags = doc.Descendants("contrastFlag");
foreach (XElement e in flags)
{
e.Value = "false";
}
doc.Save(#"c:\test2.xml");
I am writing a program that reads a XML file with Visual C#. I have a problem reading the Xml file, because it contains invalid XML symbols, for example '&'.
I have to read the XML but I can not modify the document. How can I modify the Xml file using C#? My code so far:
private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc;
doc = new XmlDocument();
doc.Load("nuevo.xml");
XmlNodeList Xpersonas = doc.GetElementsByTagName("personas");
XmlNodeList Xlista = ((XmlElement)Xpersonas[0]).GetElementsByTagName("edad");
foreach (XmlElement nodo in Xlista)
{
string edad = nodo.GetAttribute("edad");
string nombre = nodo.InnerText;
textBox1.Text = nodo.InnerXml;
}
As #EBrown suggested, one possibility would be read the file content in a string variable and replace the & symbol with the correct representation for propert XML & and then parse the XML structure. A possible solution could look like this:
var xmlContent = File.ReadAllText(#"nuevo.xml");
XmlDocument doc;
doc = new XmlDocument();
doc.LoadXml(xmlContent.Replace("&", "&"));
XmlNodeList Xpersonas = doc.GetElementsByTagName("personas");
XmlNodeList Xlista = ((XmlElement)Xpersonas[0]).GetElementsByTagName("edad");
foreach (XmlElement nodo in Xlista)
{
string edad = nodo.GetAttribute("edad");
string nombre = nodo.InnerText;
Console.WriteLine(nodo.InnerXml.Replace("&", "&"));
}
The output is:
34 & 34
If it is ok to use LINQ2XML, then the solution is even shorter, and there is no need to write the reverse(second) replace, because LINQ2XML make this for you automatically:
var xmlContent = File.ReadAllText(#"nuevo.xml");
var xmlDocument = XDocument.Parse(xmlContent.Replace("&", "&"));
var edad = xmlDocument.Root.Element("edad").Value;
Console.WriteLine(edad);
The output is the same as above.
Please, Help me Out Here
I Want to display the value of "email" from xml. my syntax works for now but it displays every value. i want to be able to display Individual (one) Values like
email: mail#mail.com
My scripts
var xml ="<?xml version='1.0' encoding='UTF-8'?>
<MemResponse>
<Phone>2554535</Phone>
<Email>mail#mail</Email>
<Number>we75546654</Number>
</MemResponse>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
foreach(XmlNode n in doc.DocumentElement)
{
string q = n.FirstChild.InnerText;
Response.Write(q);
}
Simply you can select all element by tag name by GetElementsByTagName method.
Check this :
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
var myEmails = doc.GetElementsByTagName("Email");
foreach (XmlNode mail in myEmails)
{
string mailText = mail.FirstChild.InnerText;
Response.Write(mailText);
}
I have got it from MSDN
Your code goes through each node and writes the contents of that node.
It seems like you want to match on the node name, and only write the value if its name is "email".
If thats the case, inside of your for each, try something like:
if(n.Name == "Email") {
string q = n.FirstChild.InnerText;
Response.Write(q);
}
Alternatively, you could just use a node list.
NodeList nl = doc.GetElementsByTagName("Email");
And write that.
When loading your XML Doc, use :
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());
Please try this code:
XmlDocument doc = new XmlDocument();
doc.LoadXml("YOUR_XML_PATH");
XmlNodeList email_hd= doc.GetElementsByTagName("Email");
string email=email_hd[0].InnerText;
I'm trying to read the following response. The green line states the node I need to read:
I need to read the 3rd 'result' item ALWAYS
My Code:
string location = "";
string url = "http://maps.googleapis.com/maps/api/geocode/xml?" + latitude + "," + longitude + "&sensor=true";
XmlDocument doc1 = new XmlDocument();
doc1.Load(url);
XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/GeocodeResponse[0]");
foreach (XmlNode node in nodes)
{
location = node["formatted_address"].InnerText;
}
The code doesn't give me any nodes in fact the the Node count is 0. What am I doing wrong?
The problem is in Xpath expression. To get all ther result nodes use this:
XmlNodeList nodes = root.SelectNodes("/GeocodeResponse/result");
If you want only the third result node you can use SelectSingleNode method:
var node = root.SelectSingleNode("/GeocodeResponse/result[3]");
location = node["formatted_address"].InnerText;
Update
The initial problem is in you url.You forgot latlng= part. This is an example of working url:
https://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=true
Also there could be a problem with number format, if your sytem properties have , as a double delimeter. You can use InvariantCulture to solve this. So the working example would be:
double latitude = 40.714224;
double longitude = -73.961452;
string url = String.Format(System.Globalization.CultureInfo.InvariantCulture,
"http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=true",
latitude,
longitude);
XmlDocument doc = new XmlDocument();
doc.Load(url);
XmlElement root = doc.DocumentElement;
var node = root.SelectSingleNode("/GeocodeResponse/result[3]");
var location = node["formatted_address"].InnerText;
I wanted to convert a string (which obviously is an xml) to an XmlNode in C#.While searching the net I got this code.I would like to know whether this is a good way to convert a string to XmlNode? I have to preform this conversion within a loop, so does it cause any performace issues?
XmlTextReader textReader = new XmlTextReader(new StringReader(xmlContent));
XmlDocument myXmlDocument = new XmlDocument();
XmlNode newNode = myXmlDocument.ReadNode(textReader);
Please reply,
Thanks
Alex
should be straight-forward:
string xmlContent = "<foo></foo>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlContent);
XmlNode newNode = doc.DocumentElement;
or with LINQ if that's an option:
XElement newNode = XDocument.Parse(xmlContent).Root;
The accepted answer works only for single element. XmlNode can have multiple elements like string xmlContent = "<foo></foo><bar></bar>"; (Exception: "There are multiple root elements");
To load multiple elements use this:
string xmlContent = "<foo></foo><bar></bar>";
XmlDocument doc = new XmlDocument();
doc.LoadXml("<singleroot>"+xmlContent+"</singleroot>");
XmlNode newNode = doc.SelectSingleNode("/singleroot");
XmlDocument Doc = new XmlDocument();
Doc.LoadXml(xml);