【问题标题】:Cannot create XML tag with colon in name via XElement [duplicate]无法通过 XElement 创建名称中带有冒号的 XML 标记 [重复]
【发布时间】:2018-07-20 10:43:00
【问题描述】:

我需要创建名为 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>

【问题讨论】:

    标签: c# xml rss georss


    【解决方案1】:

    geonamespace 名称前缀 latlon

    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);
    

    【讨论】:

    • 以前试过。任何想法为什么它产生&lt;lat xmlns="http://www.w3.org/2003/01/geo/wgs84_pos"&gt;1231231&lt;/lat&gt;而不是&lt;geo:lat&gt;
    • @gneric 因为它是用不同的方式编写的同一件事
    • 好的,谢谢我真的希望它是一样的,它不会有可用性问题
    • @gneric 没问题,它是有效的 xml
    • 您可以插入一个名为XNamespace.Xmlns + "geo" 的属性来获取输出中的前缀。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 2017-03-19
    • 2011-03-01
    相关资源
    最近更新 更多