c# - 无法通过 XElement 创建名称中带有冒号的 XML 标记
问题描述
我需要创建带有名称的 XML 标记geo:lat
并geo:long
用于创建 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>
解决方案
geo
是名称和名称的命名空间前缀。lat
lon
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);
推荐阅读
- python - 正则表达式在所有大写单词之前插入一个字符
- python - Django,找不到模板
- python - 在pygame中围绕中心点旋转玩家
- azure - Azure DevOps 服务连接不适用于自定义作业
- sql - 组合多个表时获取重复的主键(完全连接)
- neo4j - 在 Cypher 中获取具有特定类型的恰好一个关系的节点对,这些关系将它们相互连接
- linux - 程序在执行后挂起——未完成
- python - 使用事务时Django中的竞争条件
- sql - 在 SQL 中加入 3 个表并获取重复记录
- spring - Spring Oauth2 授权服务器为无效凭据抛出 java.lang.StackOverflowError