C# 将 XML 文件转换为多个多边形对象
C# Convert XML file to Multiple Polygon Objects
将多个 MapInfo 文件转换为一个唯一的 Shapefile,然后再次将该文件转换为 .KML 后,我得到了以下 .XML 文件。我的想法是提取每组 'coordinates' 部分,并使用它们构建多边形。
其他尝试的解决方案:
鉴于面临这种阻塞的时间过长,我尝试获取每对 'coordinates' 标签并使用 Substring 获取坐标。不幸的是,鉴于文件的大小 (>400 MB),这种肮脏的方法并不实用。
Xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema id="PruebaKML4g.schema">
<SimpleField name="FID" type="float"/>
<SimpleField name="REGION" type="float"/>
<SimpleField name="NOMBRE" type="string"/>
<SimpleField name="layer" type="string"/>
<SimpleField name="path" type="string"/>
</Schema>
<Document id="PruebaKML4g">
<name>PruebaKML4g</name>
<Placemark id="PruebaKML4g.1">
<ExtendedData>
<SchemaData schemaUrl="#PruebaKML4g.schema">
<SimpleData name="FID">5</SimpleData>
<SimpleData name="REGION">1</SimpleData>
<SimpleData name="NOMBRE">BAJA CALIFORNIA</SimpleData>
<SimpleData name="layer">LBS_REGION_1_region</SimpleData>
<SimpleData name="path">C:/Files/LBS_REGION_1_region.shp</SimpleData>
</SchemaData>
</ExtendedData>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-105.258751,21.782028,0
-105.247174,21.81173,0
-105.241826,21.809401,0
-105.236994,21.806241,0
-105.232822,21.802344,0
-105.229439,21.79783,0
-105.228552,21.796052,0
-105.228974,21.795899,0
-105.230294,21.79522,0
-105.231872,21.79511,0
-105.234048,21.79431,0
-105.235131,21.794083,0
-105.236824,21.793857,0
-105.238518,21.793295,0
-105.239365,21.792389,0
-105.240327,21.790914,0
-105.242379,21.79046,0
-105.243829,21.790459,0
-105.245644,21.788766,0
-105.247331,21.785709,0
-105.24817,21.783115,0
-105.248701,21.780372,0
-105.258751,21.782028,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
...
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-103.704559,20.767933,0
-103.702714,20.773608,0
-103.701694,20.77322,0
-103.700762,20.772672,0
-103.699944,20.77198,0
-103.699267,20.771165,0
-103.698751,20.770252,0
-103.698411,20.769268,0
-103.698258,20.768243,0
-103.698297,20.76721,0
-103.704559,20.767933,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-105.160778,20.766278,0
-105.162411,20.77201,0
-105.161328,20.77219,0
-105.160228,20.77219,0
-105.159145,20.77201,0
-105.158111,20.771656,0
-105.157159,20.771139,0
-105.156317,20.770474,0
-105.15561,20.769682,0
-105.15506,20.768786,0
-105.160778,20.766278,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-117.125814,32.524285,0
-117.125516,32.524512,0
-117.125142,32.524428,0
-117.124876,32.524169,0
-117.124754,32.524513,0
-117.124784,32.525361,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</Document>
</Document>
</kml>
我尝试使用以下代码:
主要
public T DeserializeToObject<T>(string filepath) where T : class
{
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StreamReader streamReader = new StreamReader(filepath))
{
return (T)xmlSerializer.Deserialize(streamReader);
}
}
private void Form1_Load(object sender, EventArgs e)
{
String pathKml = @"C:\PruebaKML4g.kml";
List<Kml> elementsList = DeserializeToObject<List<Kml>>(pathKml);
}
Kml.cs
/*[XmlRoot(ElementName = "kml")] changed by Mike Clark suggestion*/
[XmlRoot(ElementName = "kml", Namespace = "http://www.opengis.net/kml/2.2")]
public class Kml
{
public List<Polygon> polygons = new List<Polygon>();
}
public class Polygon
{
[XmlAttribute("outerBoundaryIs")]
public String outerBoundaryIs { get; set; }
[XmlAttribute("linearRing")]
public String linearRing { get; set; }
[XmlAttribute("coordinates")]
public String coordinates { get; set; }
}
但是,XML 文件中的 SimpleData 元素似乎干扰了我的解析,生成了以下错误
InvalidOperationException: xmlns='http://www.opengis.net/kml/2.2'> was not expected.
任何关于我的错误所在的线索将不胜感激。
经过所有这些转换步骤,也许 XML 文件格式不正确?或者可能是有这么多数据的内存错误?尝试使用低内存要求的 SAX 解析器解析文件,让它找到可能深埋在文件中的任何语法错误。你有安装 Python 吗?
python -c "import xml.sax;p=xml.sax.make_parser();p.parse(r'yourfile.xml')"
将 yourfile.xml
更改为 XML 文件的正确路径和文件名。
如果不打印任何内容,则文件在句法上是有效的。如果它打印错误,请尝试使用错误中的 line:column 信息来发现 XML.
中的错误
第 2 部分:
List<Kml> elementsList = DeserializeToObject<List<Kml>>(pathKml);
可能是错误的。 XML 文档可以有一个且只有一个根 <element>
(在这种情况下,<kml>
)所以我认为拥有一个 Kml 实例列表对解析器来说没有意义。试试这个:
Kml root = DeserializeToObject<Kml>(pathKml);
但与下一个问题相比,这是一个简单的问题,我认为您的 C# class 结构需要准确反映 XML 的层次结构。多边形在此层次结构下:
kml > Document > Document > Placemark > MultiGeometry
这意味着你需要像
这样的东西
class Kml {
Document Document;
}
class Document {
Document Document;
Placemark Placemark;
}
class Placemark {
Polygon[] MultiGeometry;
}
class Polygon {
OuterBoundaryIs outerBoundaryIs;
}
class OuterBoundaryIs {
LinearRing LinearRing;
}
class LinearRing {
string coordinates;
}
那么你需要像
这样的东西
var polygons = kml.Document.Document.Placemark.MultiGeometry;
for(int i = 0; i < polygons.Length; i++) {
var polygon = polygons[i];
string coordinates = polygon.outerBoundaryIs.LinearRing.coordinates;
// do something with coordinates
}
顺便说一句,对于这种类型的东西,一种更好的解析器是 XPath 解析器,它可以避免使用 C# classes 对 XML 结构进行建模。制作 XPath 查询需要一些实践和研究,但生成的代码更清晰,并且有一些经验是一项很好的技能。更多 XPath 参见:
https://docs.microsoft.com/en-us/dotnet/standard/data/xml/select-nodes-using-xpath-navigation
将多个 MapInfo 文件转换为一个唯一的 Shapefile,然后再次将该文件转换为 .KML 后,我得到了以下 .XML 文件。我的想法是提取每组 'coordinates' 部分,并使用它们构建多边形。
其他尝试的解决方案:
鉴于面临这种阻塞的时间过长,我尝试获取每对 'coordinates' 标签并使用 Substring 获取坐标。不幸的是,鉴于文件的大小 (>400 MB),这种肮脏的方法并不实用。
Xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema id="PruebaKML4g.schema">
<SimpleField name="FID" type="float"/>
<SimpleField name="REGION" type="float"/>
<SimpleField name="NOMBRE" type="string"/>
<SimpleField name="layer" type="string"/>
<SimpleField name="path" type="string"/>
</Schema>
<Document id="PruebaKML4g">
<name>PruebaKML4g</name>
<Placemark id="PruebaKML4g.1">
<ExtendedData>
<SchemaData schemaUrl="#PruebaKML4g.schema">
<SimpleData name="FID">5</SimpleData>
<SimpleData name="REGION">1</SimpleData>
<SimpleData name="NOMBRE">BAJA CALIFORNIA</SimpleData>
<SimpleData name="layer">LBS_REGION_1_region</SimpleData>
<SimpleData name="path">C:/Files/LBS_REGION_1_region.shp</SimpleData>
</SchemaData>
</ExtendedData>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-105.258751,21.782028,0
-105.247174,21.81173,0
-105.241826,21.809401,0
-105.236994,21.806241,0
-105.232822,21.802344,0
-105.229439,21.79783,0
-105.228552,21.796052,0
-105.228974,21.795899,0
-105.230294,21.79522,0
-105.231872,21.79511,0
-105.234048,21.79431,0
-105.235131,21.794083,0
-105.236824,21.793857,0
-105.238518,21.793295,0
-105.239365,21.792389,0
-105.240327,21.790914,0
-105.242379,21.79046,0
-105.243829,21.790459,0
-105.245644,21.788766,0
-105.247331,21.785709,0
-105.24817,21.783115,0
-105.248701,21.780372,0
-105.258751,21.782028,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
...
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-103.704559,20.767933,0
-103.702714,20.773608,0
-103.701694,20.77322,0
-103.700762,20.772672,0
-103.699944,20.77198,0
-103.699267,20.771165,0
-103.698751,20.770252,0
-103.698411,20.769268,0
-103.698258,20.768243,0
-103.698297,20.76721,0
-103.704559,20.767933,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-105.160778,20.766278,0
-105.162411,20.77201,0
-105.161328,20.77219,0
-105.160228,20.77219,0
-105.159145,20.77201,0
-105.158111,20.771656,0
-105.157159,20.771139,0
-105.156317,20.770474,0
-105.15561,20.769682,0
-105.15506,20.768786,0
-105.160778,20.766278,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-117.125814,32.524285,0
-117.125516,32.524512,0
-117.125142,32.524428,0
-117.124876,32.524169,0
-117.124754,32.524513,0
-117.124784,32.525361,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</Document>
</Document>
</kml>
我尝试使用以下代码:
主要
public T DeserializeToObject<T>(string filepath) where T : class
{
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StreamReader streamReader = new StreamReader(filepath))
{
return (T)xmlSerializer.Deserialize(streamReader);
}
}
private void Form1_Load(object sender, EventArgs e)
{
String pathKml = @"C:\PruebaKML4g.kml";
List<Kml> elementsList = DeserializeToObject<List<Kml>>(pathKml);
}
Kml.cs
/*[XmlRoot(ElementName = "kml")] changed by Mike Clark suggestion*/
[XmlRoot(ElementName = "kml", Namespace = "http://www.opengis.net/kml/2.2")]
public class Kml
{
public List<Polygon> polygons = new List<Polygon>();
}
public class Polygon
{
[XmlAttribute("outerBoundaryIs")]
public String outerBoundaryIs { get; set; }
[XmlAttribute("linearRing")]
public String linearRing { get; set; }
[XmlAttribute("coordinates")]
public String coordinates { get; set; }
}
但是,XML 文件中的 SimpleData 元素似乎干扰了我的解析,生成了以下错误
InvalidOperationException: xmlns='http://www.opengis.net/kml/2.2'> was not expected.
任何关于我的错误所在的线索将不胜感激。
经过所有这些转换步骤,也许 XML 文件格式不正确?或者可能是有这么多数据的内存错误?尝试使用低内存要求的 SAX 解析器解析文件,让它找到可能深埋在文件中的任何语法错误。你有安装 Python 吗?
python -c "import xml.sax;p=xml.sax.make_parser();p.parse(r'yourfile.xml')"
将 yourfile.xml
更改为 XML 文件的正确路径和文件名。
如果不打印任何内容,则文件在句法上是有效的。如果它打印错误,请尝试使用错误中的 line:column 信息来发现 XML.
中的错误第 2 部分:
List<Kml> elementsList = DeserializeToObject<List<Kml>>(pathKml);
可能是错误的。 XML 文档可以有一个且只有一个根 <element>
(在这种情况下,<kml>
)所以我认为拥有一个 Kml 实例列表对解析器来说没有意义。试试这个:
Kml root = DeserializeToObject<Kml>(pathKml);
但与下一个问题相比,这是一个简单的问题,我认为您的 C# class 结构需要准确反映 XML 的层次结构。多边形在此层次结构下:
kml > Document > Document > Placemark > MultiGeometry
这意味着你需要像
这样的东西class Kml {
Document Document;
}
class Document {
Document Document;
Placemark Placemark;
}
class Placemark {
Polygon[] MultiGeometry;
}
class Polygon {
OuterBoundaryIs outerBoundaryIs;
}
class OuterBoundaryIs {
LinearRing LinearRing;
}
class LinearRing {
string coordinates;
}
那么你需要像
这样的东西var polygons = kml.Document.Document.Placemark.MultiGeometry;
for(int i = 0; i < polygons.Length; i++) {
var polygon = polygons[i];
string coordinates = polygon.outerBoundaryIs.LinearRing.coordinates;
// do something with coordinates
}
顺便说一句,对于这种类型的东西,一种更好的解析器是 XPath 解析器,它可以避免使用 C# classes 对 XML 结构进行建模。制作 XPath 查询需要一些实践和研究,但生成的代码更清晰,并且有一些经验是一项很好的技能。更多 XPath 参见:
https://docs.microsoft.com/en-us/dotnet/standard/data/xml/select-nodes-using-xpath-navigation