C# 应用命名空间时,使用XElement生成的KML已损坏

C# 应用命名空间时,使用XElement生成的KML已损坏,c#,kml,xelement,C#,Kml,Xelement,我正在做一个项目,从我们的交付数据库生成一些KML数据 我很高兴使用LINQ构建KML结构,但当名称空间属性应用于节点时,我似乎无法将数据输出到字符串 这是我用来生成KML的代码: // Create a new XDocument object _xDoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes")); // Build internal kml document node XElement document = Creat

我正在做一个项目,从我们的交付数据库生成一些KML数据

我很高兴使用LINQ构建KML结构,但当名称空间属性应用于节点时,我似乎无法将数据输出到字符串

这是我用来生成KML的代码:

// Create a new XDocument object
_xDoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"));

// Build internal kml document node
XElement document = CreateKmlDocumentNode();

if (document != null)
{
    // Add data points to the kml document node
    foreach (KmlData delivery in _deliveryData)
    {
        document.Add( CreatePlacemark(delivery) );
    }
}

// Add the document node to the kml node
XElement kml = new XElement("kml",
                            document);

// ** Comment out this line and the output is generated **
kml.Add( new XAttribute("xmlns", @"http://earth.google.com/kml/2.2"));

// And finally add the kml node to the XDocument
_xDoc.Add( kml );
这是我用来生成字符串的代码:

string output;

using (var stringWriter = new StringWriter())
{
    XmlWriterSettings xws = new XmlWriterSettings();
    xws.NamespaceHandling = NamespaceHandling.OmitDuplicates;
    xws.Indent = true;

    using (var xmlTextWriter = XmlWriter.Create(stringWriter, xws))
    {
        // The Line below throws the exception when the namespace attribute is added
        _xDoc.WriteTo(xmlTextWriter);

        xmlTextWriter.Flush();
        output = stringWriter.GetStringBuilder().ToString();
    }
}
return output;
生成的异常文本:

{“无法将前缀“”从“”重新定义为”http://earth.google.com/kml/2.2'在同一开始元素标记内。}

以下是我希望数据的示例:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
  <Document>
    <name>Sample</name>
    <description><![CDATA[]]></description>

    <Style id="depot">
      <IconStyle>
        <Icon>
          <href>http://maps.gstatic.com/mapfiles/ms2/micons/rangerstation.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <Style id="pickupIcon">
      <IconStyle>
        <Icon>
          <href>http://maps.gstatic.com/mapfiles/ms2/micons/truck.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <Style id="letterIcon">
      <IconStyle>
        <Icon>
          <href>http://maps.gstatic.com/mapfiles/ms2/micons/postoffice-us.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <Placemark>
      <name>Chester Depot</name>
      <description><![CDATA[]]></description>
      <styleUrl>#depot</styleUrl>
      <Point>
        <coordinates>-2.881701,53.197021,0.000000</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>15 Hankelow Close</name>
      <description><![CDATA[<div><b><font size="4">Delivery Successful - 10:14am</font></b></div><div style="font-size:10pt"><b>Contact </b>Sam Spade</div><b style="font-size:10pt">Address </b><font size="2">15 Hankelow Close, Chester, Cheshire West and Chester CH2 2DZ, UK]]></description>
      <styleUrl>#letterIcon</styleUrl>
      <Point>
        <coordinates>-2.889466,53.199226,0.000000</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <name>45 Victoria Rd</name>
      <description><![CDATA[<div><b><font size="4">Pickup Successful - 1:24pm</font></b></div><div style="font-size:10pt"><b>Contact </b>Sam Spade</div><b style="font-size:10pt">Address </b><font size="2">Chester, Cheshire West and Chester CH2 2AX, UK]]></description>
      <styleUrl>#pickupIcon</styleUrl>
      <Point>
        <coordinates>-2.892855,53.198498,0.000000</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

样品
http://maps.gstatic.com/mapfiles/ms2/micons/rangerstation.png
http://maps.gstatic.com/mapfiles/ms2/micons/truck.png
http://maps.gstatic.com/mapfiles/ms2/micons/postoffice-us.png
切斯特仓库
#仓库
-2.881701,53.197021,0.000000
汉克洛街15号
交付成功-上午10:14联系Sam SpadeAddress 15 Hankelow Close,切斯特,柴郡西部和切斯特CH2 2DZ,英国]]>
#字母图标
-2.889466,53.199226,0.000000
维多利亚路45号
取货成功-下午1:24联系柴郡西切斯特和英国切斯特CH2 2AX的Sam SpadeAddress Chester]>
#拾音器
-2.892855,53.198498,0.000000

我通过使用库而不是从头构造KML来解决此问题。

我通过使用库而不是从头构造KML来解决此问题