Possible Duplicate:
How to set the default XML namespace for an XDocument
I am trying to write a piece of code in Asp.net C#, in order to create a KML file on-the-fly and store it in a specific path.
The code gives an error when i want to add the xmlns=”http://earth.google.com/kml/2.2″ attribute of kml tag (see below). I tried replacing xmlns with another word like “id” and it works just fine. Does it have something to do with the word “xmlns” ??! pretty strange for me.
Please provide me a solution if you understand what the problem is… Thanks!
My code:
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", ""),
new XComment("This is comment by me"),
new XElement("kml", new XAttribute("xmlns", "http://earth.google.com/kml/2.2"),
new XElement("Document",
new XElement("Name", "something"), new XElement("Placemark",
new XAttribute("id", "1"),
new XElement("title", "something"),
new XElement("description", "something"),
new XElement("LookAt",
new XElement("Longitude", "49.69"),
new XElement("Latitude", "32.345")), new XElement("Point", new XElement("Coordinates", "49.69,32.345,0"))))));
doc.Save(Server.MapPath(@"~\App_Data\markers.xml"));
The runtime error it gives:
The prefix ” cannot be redefined from ” to
‘http://earth.google.com/kml/2.2‘ within the same start element tag.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.Exception Details: System.Xml.XmlException: The prefix ” cannot be
redefined from ” to ‘http://earth.google.com/kml/2.2‘ within the same
start element tag.
The kml file i would LIKE to create:
<?xml version="1.0" encoding="utf-8"?>
<!--This is comment by me-->
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Name>something</Name>
<Placemark id="1">
<title>something</title>
<description>something</description>
<LookAt>
<Longitude>49.69</Longitude>
<Latitude>32.345</Latitude>
</LookAt>
<Point>
<Coordinates>49.69,32.345,0</Coordinates>
</Point>
</Placemark>
</Document>
</kml>
Define the namespace first
Also your XML structure is wrong,it should be like