无法通过XElement创建带有冒号的XML标签

时间:2018-07-20 10:43:00

标签: c# xml rss georss

我需要创建名称为geo:latgeo:long的XML标签,以创建GeoRSS提要。但是它会抛出

  

名称中不能包含十六进制值0x3A的':'字符。

部分代码是这样的:

 XElement("geo:lat", item.Latitude);
 XElement("geo:long", item.Longitude);

如何在C#中实现这种格式?

<?xml version="1.0"?>
<?xml-stylesheet href="/eqcenter/catalogs/rssxsl.php?feed=eqs7day-M5.xml" type="text/xsl" 
              media="screen"?>
<rss version="2.0" 
  xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" 
  xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>USGS M5+ Earthquakes</title>
 <description>Real-time, worldwide earthquake list for the past 7 days</description>
 <link>https://earthquake.usgs.gov/eqcenter/</link>
 <dc:publisher>U.S. Geological Survey</dc:publisher>
 <pubDate>Thu, 27 Dec 2007 23:56:15 PST</pubDate>
 <item>
   <pubDate>Fri, 28 Dec 2007 05:24:17 GMT</pubDate>
   <title>M 5.3, northern Sumatra, Indonesia</title>
   <description>December 28, 2007 05:24:17 GMT</description>
 <link>https://example.com</link>
   <geo:lat>5.5319</geo:lat>
   <geo:long>95.8972</geo:long>
 </item>

 

1 个答案:

答案 0 :(得分:6)

geo是名称latlon的{​​{3}}前缀。

XNamespace geo = "http://www.w3.org/2003/01/geo/wgs84_pos#";
XNamespace dc= "http://purl.org/dc/elements/1.1/";

XElement(geo + "lat", item.Latitude);
XElement(geo + "long", item.Longitude);
相关问题