首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将元素动态添加到XML文件中

将元素动态添加到XML文件中
EN

Stack Overflow用户
提问于 2014-12-15 10:50:28
回答 1查看 1.6K关注 0票数 10

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

代码语言:javascript
运行
复制
        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.
        };}

它运行得很好,看起来可能是这样的:

代码语言:javascript
运行
复制
<?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文件都有一个简短的描述。

我已经完成了一些工作,如果我在值中编写硬代码,它将如下所示:

代码语言:javascript
运行
复制
        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"}
                }
            }
        };}

它产生这个输出:

代码语言:javascript
运行
复制
<?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>

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

代码语言:javascript
运行
复制
    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,而不是包含所有对象的单个文件。

对我如何处理这个问题有什么提示吗?如果我的要求在目前的形式上过于含糊,请告诉我。

编辑:原来只是盯着它足够长的时间固定它!

以下是更新的代码:

代码语言:javascript
运行
复制
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文件:

代码语言:javascript
运行
复制
    <?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-15 14:29:21

好吧,经过几个小时的盯着它,我终于修好了这个!

结果,我可以做一个foreach(),在这里迭代我想要读取的所有文件,创建一个TileMap对象并将它添加到一个列表中,然后在foreach()之后将它写到XML中。我用我的新代码更新了OP。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27482374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档