Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add element to XML file

Tags:

c#

xml

I'm currently making an XML file using this code where I create an object which gets written to an XML file every time the method is called:

        public static void TileMapCapabilities(string title, TilePicker picker)
        {
        var dbInfo = picker.GetCapabilitiesInfo();

        TileMapObject tmo = new TileMapObject()
        {
            Title = title,
            Abstract = "",
            KeywordList = new KeywordList() {FirstLayer = ""},
            SRS = "OSGEO:41001",
            Profile = "local",
            Format = "image/png",
            BoundingBox = dbInfo.eBoundingBox,
            MapSize = dbInfo.mapSize,
            CellSize = dbInfo.cellSize,
            MaxLevel = dbInfo.level,
            Location = dbInfo.location // Not sure if this should be here. Could be practical in scenarios where the tile server is hosted locally.
        };}

It's working fine and it could looks like this:

<?xml version="1.0" encoding="utf-8"?>
<TileMapServicesObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <TileMapService>
   <Title>EivaTMS</Title>
    <Abstract>Something clever about this service</Abstract>
    <TileMaps>
      <TileMap>
        <Title>kraken.db</Title>
        <href>10.10.100.200/kraken.db?request=getcapabilities</href>
        <Profile>global-mercator</Profile>
        <SRS>OSGEO:41001</SRS>
      </TileMap>
    </TileMaps>
  </TileMapService>
</TileMapServicesObject>

Now, what I want to do is create an XML file that sort of describes the layer above the one mentioned above. In other words, I want an XML file with a short describtion of every XML file that I have above.

I've managed to do some of the work, and if I hard code in the values, it will look like this:

        public static void TileMapServicesCapabilities(int listBoxCount)
        {         

        TileMapServicesObject tmso = new TileMapServicesObject()
        {
            TileMapService = new TileMapService()
            {
                Title = "EivaTMS",
                Abstract = "Something clever about this service",
                TileMaps = new List<TileMap>
                {
                    new TileMap { Title = "title1", href = "http://title1/?request=getcapabilities", Profile = "global-mercator", SRS = "OSGEO:41001"},
                    new TileMap { Title = "title2", href = "http://title2/?request=getcapabilities", Profile = "global-mercator", SRS = "OSGEO:41001"},
                    new TileMap { Title = "title3", href = "http://title3/?request=getcapabilities", Profile = "global-mercator", SRS = "OSGEO:41001"}
                }
            }
        };}

Which produces this output:

<?xml version="1.0" encoding="utf-8"?>
<TileMapServicesObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <TileMapService>
    <Title>EivaTMS</Title>
    <Abstract>Something clever about this service</Abstract>
    <TileMaps>
      <TileMap>
        <Title>title1</Title>
        <href>http://title1/?request=getcapabilities</href>
        <Profile>global-mercator</Profile>
        <SRS>OSGEO:41001</SRS>
      </TileMap>
      <TileMap>
        <Title>title2</Title>
        <href>http://title2/?request=getcapabilities</href>
        <Profile>global-mercator</Profile>
        <SRS>OSGEO:41001</SRS>
      </TileMap>
      <TileMap>
        <Title>title3</Title>
        <href>http://title3/?request=getcapabilities</href>
        <Profile>global-mercator</Profile>
        <SRS>OSGEO:41001</SRS>
      </TileMap>
    </TileMaps>
  </TileMapService>
</TileMapServicesObject>

Now, what I want to do is create and add the elements dynamically. This means that for every XML file I have created by the first method TileMapCapabilities, I want to create a object and add it to the TileMapServicesObject. It's probably also worth mentioning how the classes look that contains the information in the objects being created:

    public class TileMapServicesObject
    {
        public TileMapService TileMapService { get; set; }
    }

    public class TileMapService
    {
        public string Title { get; set; }
        public string Abstract { get; set; }
        public List<TileMap> TileMaps { get; set; }
    }

    public class TileMap
    {
        public string Title { get; set; }
        public string href { get; set; }
        public string Profile { get; set; }
        public string SRS { get; set; }
    }

I've been trying with a foreach loop creating a TileMap object for every TileMap I'm adding to the service, but this just created a new TileMapServicesObject for each iteration, instead of a single file containing all the objects.

Any hints to how I can approach this problem? Please let me know if my request is too vague in its current form.


EDIT: Turns out just staring at it for long enough fixed it!

Here's the updated code:

public static void TileMapServicesCapabilities()
    {
        TileMapServicesObject tmso = new TileMapServicesObject();
        List<string> pathList = new List<string>(); 
        pathList = Directory.GetFiles(path + @"Tilemaps\").ToList();
        List<TileMap> tmList = new List<TileMap>();
        TileMap tm = new TileMap();
        string title = "";
        string profile = "";
        string srs = "";

        foreach (var p in pathList)
        {
            var xRead = XDocument.Load(p);

            var xd = (from el in xRead.Descendants("TileMapObject")
                    select new 
                    {
                        Title = el.Element("Title").Value,
                        Profile = el.Element("Profile").Value,
                        SRS = el.Element("SRS").Value
                    }).SingleOrDefault();

            title = xd.Title;
            profile = xd.Profile;
            srs = xd.SRS;

            tm = new TileMap()
            {
                Title = title,
                Profile = profile,
                SRS = srs,
                href = "http://10.10.100.200/" + title + "?request=getcapabilities"
            };

            tmList.Add(tm);
        }

        tmso = new TileMapServicesObject()
        {
            TileMapService = new TileMapService()
            {
                Title = "EivaTMS",
                Abstract = "Something clever about this service",
                TileMaps = tmList
            }
        };

Which gives me this beautiful XML file:

    <?xml version="1.0" encoding="utf-8"?>
<TileMapServicesObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <TileMapService>
    <Title>EivaTMS</Title>
    <Abstract>Something clever about this service</Abstract>
    <TileMaps>
      <TileMap>
        <Title>title1</Title>
        <href>http://title1?request=getcapabilities</href>
        <Profile>local</Profile>
        <SRS>OSGEO:41001</SRS>
      </TileMap>
      <TileMap>
        <Title>title2</Title>
        <href>http://title2?request=getcapabilities</href>
        <Profile>local</Profile>
        <SRS>OSGEO:41001</SRS>
      </TileMap>
      <TileMap>
        <Title>title3</Title>
        <href>http://title3?request=getcapabilities</href>
       <Profile>local</Profile>
       <SRS>OSGEO:41001</SRS>
      </TileMap>
      <TileMap>
        <Title>title4</Title>
        <href>http://title4?request=getcapabilities</href>
        <Profile>local</Profile>
        <SRS>OSGEO:41001</SRS>
      </TileMap>
    </TileMaps>
  </TileMapService>
</TileMapServicesObject>
like image 804
Khaine775 Avatar asked Dec 15 '14 10:12

Khaine775


1 Answers

Alright, I managed to fix this one after hours and hours of staring at it!

Turns out I can just make a foreach() where I iterate through all the files I want to read from, create a TileMap object and add it to a List which I then write to XML after the foreach(). I've updated the OP with my new code.

like image 85
Khaine775 Avatar answered Oct 17 '22 07:10

Khaine775