我有一个包含以下数据的数据城市(我发布的60000项中只有3项)。
我想从提供的数据中创建一个xml(KML)。
如何创建通用解决方案?
也许创建类并添加属性然后加载到xml。
这将是生成的XML。我只发布一部分
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:json="http://www.w3.org/2005/Json">
<Folder>
<name>USA</name>
<open>1</open>
<Folder>
<name>Corperation</name>
<open>1</open>
<Folder>
<name>LARGE</name>
<Folder>
<name>WORKING</name>
<Document>
<name>POINTS</name>
<StyleMap id="Licerio0_0">
<Pair>
<key>normal</key>
<styleUrl>#Licerio0_0N</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#Licerio0_0H</styleUrl>
</Pair>
</StyleMap>
<Style id="Licerio0_0N">
<IconStyle>
<color>ff00ff00</color>
<scale>0.4</scale>
<Icon>
http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png
</Icon>
</IconStyle>
<LabelStyle>
<color>ff00ff00</color>
<scale>0.6</scale>
</LabelStyle>
<BalloonStyle>
<text>$[description]</text>
</BalloonStyle>
<LineStyle>
<antialias>0</antialias>
</LineStyle>
<PolyStyle>
<fill>0</fill>
<outline>0</outline>
</PolyStyle>
</Style>
<Placemark>
<name>NEWYORK</name>
<description>
<![CDATA[<img src="http://upload.wikimedia.org/wikipedia/commons/USA.png"width="47" height="67">
<br>RSID = <b>NEWYORK</b><ul>
<li>PIN = 676306
<li>LONGITUDE = 54.4251
<li>LATITUDE = 25.6011
<li>SCALE = 0.7
<li>TOTAL TILT = 4
<li>CH = 87
<li>REGION = NEM</ul></li>]]>
</description>
<styleUrl>#Licerio0_0</styleUrl>
<Point>
<gx:drawOrder>3</gx:drawOrder>
<coordinates>54.4251,25.6011,0</coordinates>
</Point>
</Placemark>
</Document>
*
*<Document>
<name>LINES</name>
<Style id="Zamoras0_0">
</Style>
<Placemark>
<name>***Site Id***</name>
<styleUrl>#Zamoras0_0</styleUrl>
<MultiGeometry>
<LineString>
<gx:drawOrder>3</gx:drawOrder>
<coordinates>***LONGITUDE***,***LATITUDE***,***SCALE***</coordinates>
</LineString>
</MultiGeometry>
</Placemark>
</Document>**
</Folder>
</Folder>
</Folder>
.
.
ETC.......
</Folder>
</kml>
修改
模板中的小变化,LINES
有两个文档,POINTS
有两个文档。如果我有内部街道,那么它应该添加到文档部分的地标内。我正在编辑我的问题。如何要做到这一点?如果我使用此代码它是重复的
dt.Columns.Add("countryname", typeof(string));
dt.Columns.Add("citytype", typeof(string));
dt.Columns.Add("size", typeof(string));
dt.Columns.Add("status", typeof(string));
dt.Columns.Add("cityname", typeof(string));
dt.Columns.Add("pin", typeof(int));
dt.Columns.Add("LONGITUDE", typeof(double));
dt.Columns.Add("LATITUDE", typeof(double));
dt.Columns.Add("SCALE", typeof(double));
dt.Columns.Add("TOTAL", typeof(int));
dt.Columns.Add("TILT", typeof(string));
dt.Columns.Add("CH", typeof(string));
dt.Columns.Add("REGION", typeof(string));
dt.Columns.Add("streets", typeof(string));
dt.Rows.Add(new object[] { "USA", "Corperation", "LARGE", "WORKING", "newyork", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","firststreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "LARGE", "WORKING", "newyork", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","downtownstreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "LARGE", "WORKING", "chicago", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","chicstreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "LARGE", "WORKING", "la", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","lastreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "MEDIUM", "WORKING", "buffalo", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","buffalostreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "MEDIUM", "WORKING", "buffalo", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","cowstreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "MEDIUM", "WORKING", "denver", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","street" });
dt.Rows.Add(new object[] { "USA", "Corperation", "MEDIUM", "NOT WORKING", "lasvegas", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","street" });
dt.Rows.Add(new object[] { "USA", "Corperation", "SMALL", "WORKING", "trenton", 676306, 24.4251, 25.6011, 0.0, 87, "NEM" ,"street"});
dt.Rows.Add(new object[] { "USA", "Village", "SMALL", "WORKING", "albany", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","street" });
dt.Rows.Add(new object[] { "USA", "Village", "SMALL", "WORKING", "hartford", 676306, 24.4251, 25.6011, 0.0, 87, "NEM","street" });
dt.Rows.Add(new object[] { "SPAIN", "Corperation", "MEDIUM", "WORKING", "BARCELONA", 11111, 34.4251, 25.6011, 0.0, 82, "LEV" });
dt.Rows.Add(new object[] { "ITALY", "Corperation", "LARGE", "WORKING", "ITALY", 21111, 14.4251, 15.6011, 0.0, 80, "MIR" });
答案 0 :(得分:1)
在任何人对这个极不寻常的解决方案给出负面评价之前,请理解这些问题。 1)多个Folder嵌套标签 2)html文本
我使用xml模板文件来读取初始xml。这个xml很复杂,所以我认为从头开始创建xml并不明智。
将以下xml模板用于主文件
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:json="http://www.w3.org/2005/Json">
<Folder>
<name>***COUNTRY***</name>
<open>1</open>
<Folder>
<name>***CITY TYPE***</name>
<open>1</open>
<Folder>
<name>***SIZE***</name>
<Folder>
<name>***STATUS***</name>
<Document>
<name>POINTS</name>
<StyleMap id="Licerio0_0">
<Pair>
<key>normal</key>
<styleUrl>#Licerio0_0N</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#Licerio0_0H</styleUrl>
</Pair>
</StyleMap>
<Style id="Licerio0_0N">
<IconStyle>
<color>ff00ff00</color>
<scale>0.4</scale>
<Icon>
http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png
</Icon>
</IconStyle>
<LabelStyle>
<color>ff00ff00</color>
<scale>0.6</scale>
</LabelStyle>
<BalloonStyle>
<text>$[description]</text>
</BalloonStyle>
<LineStyle>
<antialias>0</antialias>
</LineStyle>
<PolyStyle>
<fill>0</fill>
<outline>0</outline>
</PolyStyle>
</Style>
<Placemark>
<name>***CITY NAME***</name>
<description>
<![CDATA[<img src="http://upload.wikimedia.org/wikipedia/commons/***COUNTRY***.png"width="47" height="67">
<br>RSID = <b>***CITY NAME***</b><ul>
<li>PIN = ***PIN***
<li>LONGITUDE = ***LONGITUDE***
<li>LATITUDE = ***LATITUDE***
<li>SCALE = ***SCALE***
<li>TOTAL TILT = ***TILT***
<li>CH = ***CH***
<li>REGION = ***REGION***</ul></li>]]>
</description>
<styleUrl>#Licerio0_0</styleUrl>
<Point>
<gx:drawOrder>3</gx:drawOrder>
<coordinates>***LONGITUDE***,***LATITUDE***,***SCALE***</coordinates>
</Point>
</Placemark>
</Document>
</Folder>
</Folder>
</Folder>
</Folder>
</kml>
使用以下模板作为第二个文件
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:json="http://www.w3.org/2005/Json">
<name>LINES</name>
<Style id="Zamoras0_0">
</Style>
<Placemark>
<name>***Site Id***</name>
<styleUrl>#Zamoras0_0</styleUrl>
<MultiGeometry>
<LineString>
<gx:drawOrder>3</gx:drawOrder>
<coordinates>***LONGITUDE***,***LATITUDE***,***SCALE***</coordinates>
</LineString>
</MultiGeometry>
</Placemark>
</kml>
然后使用下面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication9
{
class Program
{
const string FILENAME1 = @"c:\temp\test1.xml";
const string FILENAME2 = @"c:\temp\test2.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("countryname", typeof(string));
dt.Columns.Add("citytype", typeof(string));
dt.Columns.Add("size", typeof(string));
dt.Columns.Add("status", typeof(string));
dt.Columns.Add("cityname", typeof(string));
dt.Columns.Add("pin", typeof(int));
dt.Columns.Add("LONGITUDE", typeof(double));
dt.Columns.Add("LATITUDE", typeof(double));
dt.Columns.Add("SCALE", typeof(double));
dt.Columns.Add("TOTAL", typeof(int));
dt.Columns.Add("TILT", typeof(string));
dt.Columns.Add("CH", typeof(string));
dt.Columns.Add("REGION", typeof(string));
dt.Columns.Add("streets", typeof(string));
dt.Rows.Add(new object[] { "USA", "Corperation", "LARGE", "WORKING", "newyork", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "firststreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "LARGE", "WORKING", "newyork", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "downtownstreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "LARGE", "WORKING", "chicago", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "chicstreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "LARGE", "WORKING", "la", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "lastreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "MEDIUM", "WORKING", "buffalo", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "buffalostreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "MEDIUM", "WORKING", "buffalo", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "cowstreet" });
dt.Rows.Add(new object[] { "USA", "Corperation", "MEDIUM", "WORKING", "denver", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "street" });
dt.Rows.Add(new object[] { "USA", "Corperation", "MEDIUM", "NOT WORKING", "lasvegas", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "street" });
dt.Rows.Add(new object[] { "USA", "Corperation", "SMALL", "WORKING", "trenton", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "street" });
dt.Rows.Add(new object[] { "USA", "Village", "SMALL", "WORKING", "albany", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "street" });
dt.Rows.Add(new object[] { "USA", "Village", "SMALL", "WORKING", "hartford", 676306, 24.4251, 25.6011, 0.0, 87, "NEM", "", "", "street" });
dt.Rows.Add(new object[] { "SPAIN", "Corperation", "MEDIUM", "WORKING", "BARCELONA", 11111, 34.4251, 25.6011, 0.0, 82, "LEV" });
dt.Rows.Add(new object[] { "ITALY", "Corperation", "LARGE", "WORKING", "ITALY", 21111, 14.4251, 15.6011, 0.0, 80, "MIR" });
XDocument doc1 = XDocument.Load(FILENAME1);
XElement kml = (XElement)doc1.FirstNode;
XNamespace ns = kml.Name.Namespace;
XElement document = doc1.Descendants().Where(x => x.Name.LocalName == "Document").FirstOrDefault();
string documentStr = document.ToString();
kml.RemoveAll();
XDocument doc2 = XDocument.Load(FILENAME2);
XElement linesDoc = (XElement)doc2.FirstNode;
string linesStr = linesDoc.ToString();
var countryRows = dt.AsEnumerable().GroupBy(x => x.Field<string>("countryname"));
foreach (var country in countryRows)
{
XElement newCountry = new XElement(ns + "Folder", new object[] {
new XElement(ns + "name", country.Key),
new XElement(ns + "open", 1)
});
kml.Add(newCountry);
var cityTypeRows = country.GroupBy(x => x.Field<string>("citytype"));
foreach (var cityTypeRow in cityTypeRows)
{
XElement newCityType = new XElement(ns + "Folder", new object[] {
new XElement(ns + "name", cityTypeRow.Key),
new XElement(ns + "open", 1)
});
newCountry.Add(newCityType);
var citySizeRows = cityTypeRow.GroupBy(x => x.Field<string>("size"));
foreach (var citySizeRow in citySizeRows)
{
XElement newCitySize = new XElement(ns + "Folder", new object[] {
new XElement(ns + "name", citySizeRow.Key)
});
newCityType.Add(newCitySize);
var cityStatusRows = cityTypeRow.GroupBy(x => x.Field<string>("status"));
foreach (var cityStatusRow in cityStatusRows)
{
XElement newStatus = new XElement(ns + "Folder", new object[] {
new XElement(ns + "name", cityStatusRow.Key)
});
newCitySize.Add(newStatus);
var cityRows = cityTypeRow.GroupBy(x => x.Field<string>("cityname"));
foreach (var city in cityRows)
{
string newDocumentStr = documentStr;
newDocumentStr = newDocumentStr.Replace("***CITY NAME***", city.Key);
newDocumentStr = newDocumentStr.Replace("***PIN***", city.FirstOrDefault().Field<int>("pin").ToString());
newDocumentStr = newDocumentStr.Replace("***LONGITUDE***", city.FirstOrDefault().Field<double>("LONGITUDE").ToString());
newDocumentStr = newDocumentStr.Replace("***LATITUDE***", city.FirstOrDefault().Field<double>("LATITUDE").ToString());
newDocumentStr = newDocumentStr.Replace("***SCALE***", city.FirstOrDefault().Field<double>("SCALE").ToString());
newDocumentStr = newDocumentStr.Replace("***TOTAL***", city.FirstOrDefault().Field<int>("TOTAL").ToString());
newDocumentStr = newDocumentStr.Replace("***TILT***", city.FirstOrDefault().Field<string>("TILT"));
newDocumentStr = newDocumentStr.Replace("***CH***", city.FirstOrDefault().Field<string>("CH"));
newDocumentStr = newDocumentStr.Replace("***REGION***", city.FirstOrDefault().Field<string>("REGION"));
XElement newCity = XElement.Parse(newDocumentStr);
newCitySize.Add(newCity);
foreach (var street in city)
{
if (street.Field<object>("streets") != null)
{
string newlinesStr = linesStr;
newlinesStr = newlinesStr.Replace("***LONGITUDE***", street.Field<double>("LONGITUDE").ToString());
newlinesStr = newlinesStr.Replace("***LATITUDE***", street.Field<double>("LATITUDE").ToString());
newlinesStr = newlinesStr.Replace("***SCALE***", street.Field<double>("SCALE").ToString());
newlinesStr = newlinesStr.Replace("***Site Id***", street.Field<string>("streets").ToString());
XElement newLine = XElement.Parse(newlinesStr);
newCity.Add(newLine.Elements());
}
}
}
}
}
}
}
}
}
}