动态添加元素到XML文件

凯恩775

我目前正在使用此代码制作XML文件,在该代码中,我创建了一个对象,每次调用该方法时该对象都会写入XML文件:

        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.
        };}

它工作正常,可能看起来像这样:

<?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>

现在,我要做的是创建一个XML文件,该文件描述了上面提到的那一层之上的层。换句话说,我想要一个XML文件,并对上面的每个XML文件进行简短描述。

我已经设法完成了一些工作,如果我对值进行硬编码,它将看起来像这样:

        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"}
                }
            }
        };}

产生以下输出:

<?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>

现在,我要做的是动态创建和添加元素。这意味着对于第一种方法创建的每个XML文件TileMapCapabilities,我都想创建一个对象并将其添加到中TileMapServicesObject可能还值得一提的是,这些类的外观包含正在创建的对象中的信息:

    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; }
    }

我一直在尝试foreach循环,为要添加到服务中的每个TileMap创建一个TileMap对象,但这只是为每次迭代创建了一个新的TileMapServicesObject,而不是包含所有对象的单个文件。

关于如何解决此问题的任何提示?如果我的要求在目前的形式上过于含糊,请告诉我。


编辑:原来只是盯着它足够长时间来修复它!

这是更新的代码:

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
            }
        };

这给了我这个漂亮的XML文件:

    <?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>
凯恩775

好吧,经过数小时的盯着它,我设法解决了这个问题!

事实证明,我可以仅创建一个foreach(),在其中迭代要读取的所有文件,创建一个TileMap对象,并将其添加到List中,然后在foreach()之后写入XML。我已经用新代码更新了OP。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

动态添加元素到布局

来自分类Dev

动态添加元素到页面顶部

来自分类Dev

在XML文件中添加元素

来自分类Dev

JAXB动态添加元素

来自分类Dev

在现有的xml文件python中添加元素

来自分类Dev

如何在XML文件中添加元素?

来自分类Dev

动态添加元素上的jQuery CSS()

来自分类Dev

动态在图像顶部添加元素

来自分类Dev

在Web组件内动态添加元素

来自分类Dev

qTip和动态添加元素的问题

来自分类Dev

动态添加元素上的jQuery CSS()

来自分类Dev

使用angularJS动态添加元素

来自分类Dev

HTML:使用JS动态添加元素

来自分类Dev

动态添加元素上的click事件

来自分类Dev

更改动态添加元素的背景

来自分类Dev

向 Angularjs 动态添加元素

来自分类Dev

javascript如何使用按钮动态添加元素到表单标签

来自分类Dev

如何添加元素到BSONArray?

来自分类Dev

从函数参数添加元素到列表

来自分类Dev

如何添加元素到矩阵?

来自分类Dev

用户添加元素到列表

来自分类Dev

xml模式。如何添加元素

来自分类Dev

动态添加元素上的jQuery选择器

来自分类Dev

动态地向对象添加元素

来自分类Dev

在Groovy中向ArrayList动态添加元素

来自分类Dev

添加元素动态后的jQuery绑定事件

来自分类Dev

为什么不按角度动态添加元素

来自分类Dev

在C#中向UI动态添加元素

来自分类Dev

如何动态地向Source添加元素?