0

I'm writing a c# console program that reads a configuration file and a google map example found in google dev website.. I copied the html content into a local template file and put placeholders for string.format (notice the {1}, {2} in code snipped below.

I then read the template from the local file system and use the code below to populate the placeholders. The problem is that the string.format keeps throwing error saying Input string was not in a correct format. If I replace the template with a blank file, the Input string was not in a correct format. is not thrown. There are no exceptions. Thus, the problem is with the html template below and not the code. Can someone spot the issue with the template that make so that it doesn't work with string.format? I'm guessing that it has to do with { and } so I moved around any double (i.e. {{) but that didn't help.

Code

  var res = string.Format(template,
                ConfigurationManager.AppSettings["PageTitle"],
                ConfigurationManager.AppSettings["zoom"],
                ConfigurationManager.AppSettings["CenterLat"],
                ConfigurationManager.AppSettings["CenterLongi"],
                ConfigurationManager.AppSettings["mapTypeId"],
                 sb.ToString(),
                ConfigurationManager.AppSettings["strokeColor"],
                ConfigurationManager.AppSettings["strokeOpacity"],
                ConfigurationManager.AppSettings["strokeWeight"],
                ConfigurationManager.AppSettings["fillColor"],
                ConfigurationManager.AppSettings["fillOpacity"],
                  ConfigurationManager.AppSettings["GoogleMapBaseUrl"].Replace("_KEY_",          ConfigurationManager.AppSettings["APIKey"])

                );

HTML Template

<!DOCTYPE html>
<html>
 <head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>{0}</title>
<style>
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }

    #map {
        height: 100%;
    }
</style>
</head>
<body>
<div id="map"></div>
<script>


function initMap() {
 var map = new google.maps.Map(document.getElementById('map'), {
  zoom: {1},
  center: {  
      lat: {2}, lng: {3}   
  },
  mapTypeId: {4}
 });

 // Define the LatLng coordinates for the polygon's path.
  var triangleCoords = [

 {5}
 ];

 // Construct the polygon.
 var bermudaTriangle = new google.maps.Polygon(

  {
  paths: triangleCoords,
  strokeColor: '{6}',
strokeOpacity: {7},
strokeWeight: {8},
fillColor: '{9}',
fillOpacity: {10}


  });
  bermudaTriangle.setMap(map);
}

 </script>
 <script async defer
        src="{11}"></script>
 </body>
  </html>
Zuzlx
  • 1,246
  • 14
  • 33
  • Maybe you should consider using a StringBuilder... – rory.ap Dec 08 '15 at 18:02
  • @roryap Thanks...but and then what? Keep doing `sb.Append("blah blah").Append("blah blah")? That's kind of boring – Zuzlx Dec 08 '15 at 18:03
  • 1
    Did you notice that your function initMap is followed by a curly brace? What do you think the string format will do when it try to parse that point? – Steve Dec 08 '15 at 18:03
  • My thinking is that the string.format is looking {1}, {2}, {n}....but I could be wrong – Zuzlx Dec 08 '15 at 18:05
  • Side note: trying to provide [MCVE] (notice "minimal") may help with clarifying problem. At this point it looks like duplicate of at least one ("Escape {") problem. – Alexei Levenkov Dec 08 '15 at 18:13

1 Answers1

1

The presence of curly braces as literals in your strings leads the string.Format to fail in its parsing. You should use this syntax in your template
(showing just a part of your code)

function initMap() {{
 var map = new google.maps.Map(document.getElementById('map'), {{
  zoom: {1},
  center: {{  
      lat: {2}, lng: {3}   
  }},
  mapTypeId: {4}
 }});
Steve
  • 213,761
  • 22
  • 232
  • 286