gpt4 book ai didi

c# - 动态添加元素到 XML 文件

转载 作者:可可西里 更新时间:2023-11-01 09:15:30 26 4
gpt4 key购买 nike

我目前正在使用这段代码制作一个 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 对象并将其添加到一个列表,然后我在 foreach() 之后将其写入 XML。我已经用我的新代码更新了 OP。

关于c# - 动态添加元素到 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27482374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com