[英]Dynamically add element to XML file
我目前正在使用這段代碼創建一個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>
現在,我想要做的是動態創建和添加元素。 這意味着對於我通過第一個方法TileMapCapabilities
創建的每個XML文件,我想創建一個對象並將其添加到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>
好吧,我設法在幾個小時后盯着它來修復這個!
事實證明,我可以創建一個foreach(),在其中迭代我想要讀取的所有文件,創建一個TileMap對象並將其添加到List中,然后在foreach()之后寫入XML。 我用我的新代碼更新了OP。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.