[英]Dynamically add element to XML file
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: 我目前正在使用这段代码创建一个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.
};}
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. 现在,我想要做的是创建一个XML文件,该文件描述了上面提到的层之上的层。 In other words, I want an XML file with a short describtion of every XML file that I have above. 换句话说,我想要一个XML文件,其中简要描述了我上面提到的每个XML文件。
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
. 这意味着对于我通过第一个方法TileMapCapabilities
创建的每个XML文件,我想创建一个对象并将其添加到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. 我一直在尝试使用foreach循环为我添加到服务的每个TileMap创建一个TileMap对象,但这只是为每次迭代创建了一个新的TileMapServicesObject,而不是包含所有对象的单个文件。
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文件:
<?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>
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(). 事实证明,我可以创建一个foreach(),在其中迭代我想要读取的所有文件,创建一个TileMap对象并将其添加到List中,然后在foreach()之后写入XML。 I've updated the OP with my new code. 我用我的新代码更新了OP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.