2

maybe a stupid question here but my html skills are faded now.. :)

I am creating a KML file with a button click and all the code for the KML tags is there. What I need is using more than one tags. It will be hardcoded in the beginning of the KML. The KML should look something like this:

KML with 2 Style id

So this code is generated from the C# code behind. So the question is how to get

<Style id= "randomColorIcon">

and then close the tag properly like /Style without having the id part?

In the C# code I had something like this but I just found that it doesn't read the other Style id tags

kml.WriteStartElement("Style");
  kml.WriteElementString("id", "randomColorIcon"); //not suitable for more than one Style tags
  kml.WriteStartElement("IconStyle");
  kml.WriteStartElement("Icon");
  kml.WriteElementString("href",      "http://maps.google.com/mapfiles/kml/pal3/icon23.png");
  kml.WriteEndElement(); //</Icon>
  kml.WriteEndElement(); //</IconStyle> ??
  kml.WriteEndElement(); //</Style> 

Thanks in advance :)

Pepys
  • 981
  • 7
  • 16
  • 34
  • I'm afraid you don't have your terminology clear. Style is the name of the element. The element has a start tag `` and consists of those two tags and everything in between. The word `Style` itself if not a tag, nor is the text `Style id="id"` treated as one word. What you call "the id part" is an attribute, it's not part of the element name as such. – Mr Lister Dec 23 '11 at 10:12

2 Answers2

3

As far as XML is concerned:

<Style id= "randomColorIcon"></Style>

And:

<Style id= "randomColorIcon" />

Are equivalent.

In that respect, the following will produce the semantic equivalent of what you are asking for:

kml.WriteStartElement("Style");
kml.WriteAttributeString("id", "randomColorIcon");
kml.WriteEndElement();
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I think it's not. I tried before, I tried just now... It doesn't read the first Style tag... And I'm sure I don't have error in the syntax. Otherwise, when I try to edit the KML manually and write the id INSIDE like – Pepys Dec 23 '11 at 10:02
  • 1
    @Pepys - I don't know what you mean. "It doesn't read the first Style tag" - correct. An `XmlWriter` _writes_ XML, not reads it. I don't know what "works just fine", as you didn't describe what it is that should work or not. – Oded Dec 23 '11 at 10:05
  • Sorry.. sometimes I suck in explaining in a normal way :) What I meant by "doesn't read.." is that when I open the KML file in Google Earth, I don't see my picture but the yellow placemark icon instead. I will try again now: The way I create the Style tag in my code behind is: kml.WriteStartElement("Style"); kml.WriteElementString("id", "randomColorIcon"); ... kml.WriteEndElement(); // Then the same thing with different picture. And the generated KML file looks fine but when I open it in GE, i don't get the second picture (ie. in the second style tag). Can you understand me now ? :) – Pepys Dec 23 '11 at 10:10
  • Then when I edited the KML code manually and remove the ... and move it in – Pepys Dec 23 '11 at 10:12
  • @Pepys - Did you compare the markup between a picture that works and one that doesn't? What is different when you edit the file manually? – Oded Dec 23 '11 at 10:12
  • Yes the markup is the same. When I edited it and remove the ... and move it in – Pepys Dec 23 '11 at 10:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6085/discussion-between-pepys-and-oded) – Pepys Dec 23 '11 at 10:43
2

You'll need WriteAttributeString.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • Can you try to be more specific. There are 3 different arguments for this method. With 2, 3 and 4 strings. I'm not sure which should I use and how to get the quotation marks in the – Pepys Dec 23 '11 at 09:56
  • @Pepys - If you go to MSDN, the documentation for each overload explains what each parameter is for. – Oded Dec 23 '11 at 10:01
  • 1
    If you want to know what the different variants of a function do, there's only one thing to it. Test! Create a new project if necessary, write something to the output using the three different variants and see what has come out after you run it. – Mr Lister Dec 23 '11 at 10:15
  • Got it :) kml.WriteStartElement("Style"); kml.WriteAttributeString("id", "randomColorIcon2"); This will produce proper Style element like – Pepys Dec 23 '11 at 12:26