将命名空间添加到 SyndicatedFeed 而不是单个元素?
我有一个这样的类:
public static class MyFeedExtensions
{
private readonly static XNamespace _namespace = XNamespace.Get(@"http://mynamespace");
public static XElement MyElement(string value)
{
return new XElement(_namespace + "MyElement", value);
}
}
我使用它来生成带有自定义扩展的 Atom Feed:
var feed = new SyndicationFeed();
feed.ElementExtensions.Add(MyFeedExtensions.MyElement("Testing!"));
这工作正常,除了 feed 将我的命名空间添加到元素中:
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">Hello World!</title>
<id>00000000-0000-0000-0000-000000000000</id>
<updated>2011-03-01T01:00:53Z</updated>
<MyElement xmlns="http://mynamespace">Testing!</MyElement>
</feed>
是否有一种方法可以使用 feed 注册命名空间,以得到这样的输出?
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:my="http://mynamespace">
<title type="text">Hello World!</title>
<id>00000000-0000-0000-0000-000000000000</id>
<updated>2011-03-01T01:00:53Z</updated>
<my:MyElement>Testing!</my:MyElement>
</feed>
理想情况下,当我有带有 ElementExtensions 的 Syndicates 时,我希望这也能工作,因为提要应该了解所有不同的命名空间。
(编辑:这纯粹是为了减少 XML 的大小并使其更易于人类阅读)
I have a class like this:
public static class MyFeedExtensions
{
private readonly static XNamespace _namespace = XNamespace.Get(@"http://mynamespace");
public static XElement MyElement(string value)
{
return new XElement(_namespace + "MyElement", value);
}
}
I'm using it to generate an Atom Feed with custom Extensions:
var feed = new SyndicationFeed();
feed.ElementExtensions.Add(MyFeedExtensions.MyElement("Testing!"));
This works fine, except that the feed adds my namespace to the element:
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">Hello World!</title>
<id>00000000-0000-0000-0000-000000000000</id>
<updated>2011-03-01T01:00:53Z</updated>
<MyElement xmlns="http://mynamespace">Testing!</MyElement>
</feed>
Is there a way to register a namespace with the feed instead, to get an output like this?
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:my="http://mynamespace">
<title type="text">Hello World!</title>
<id>00000000-0000-0000-0000-000000000000</id>
<updated>2011-03-01T01:00:53Z</updated>
<my:MyElement>Testing!</my:MyElement>
</feed>
Ideally, I would like this also to work when I have SyndicationItems with ElementExtensions, as the feed should know about all the various namespaces.
(Edit: This is purely to reduce the Size of the XML and to make it easier to read for humans)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 这个问题 中找到答案并对其进行修改:
基本上:使用以下属性注册
xmlns:my
属性feed 中,它会自动获取元素上的命名空间,即使它们被添加到 feed 内的SynminationItem
中也是如此。晦涩,但整洁!
Found the answer in this question and adapted it:
Basically: Register a
xmlns:my
Attribute with the feed, it will pick up the namespace automatically on the elements even if they are added to aSyndicationItem
within the feed.Obscure, but neat!