如何正确使用带有自定义扩展的 xmlns 命名空间到 .NET SyndicatedFeeds?
我正在使用 .NET SyndicateFeed 类,并使用 SyndicateItem.ElementExtensions.Add() 添加了一些我自己的扩展,并将 SyndicateItem.Content 设置为某些 Xml 内容。
我的问题是我的命名空间在 XML 输出中多次出现。理想情况下,我会将 xmlns 属性应用于根节点,并在整个文档中使用其别名。
我已经看到讨论使用 SyndicateFeed.AttributeExtensions 的示例,如 此处。例如:
feed.AttributeExtensions.Add(
new System.Xml.XmlQualifiedName("myns", "http://www.w3.org/2000/xmlns"),
"http://myNamespace.com");
但是,这些示例都没有展示稍后如何使用名称空间。例如,以下是我扩展提要的两种方法:
XNamespace myNs = "http://myNamespace.com";
SyndicationItem item = new SyndicationItem();
XElement myMetadata = new XElement(myNs + "metadata");
myMetadata.Add(new XElement(myNs + "meta1", "value1"));
myMetadata.Add(new XElement(myNs + "meta2", "value2"));
item.Content = SyndicationContent.CreateXmlContent(myMetadata);
XElement myExtensions = new XElement(myNs + "myExtensions");
myExtensions.Add(new XElement(myNs + "ext1", "value1"));
myExtensions.Add(new XElement(myNs + "ext2", "value2"));
item.ElementExtensions.Add(myExtensions);
希望我错过了一些简单的东西。使用上面的 AttribuetExtensions.Add() 方法,我的提要具有以下初始 XML:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel p3:myns="http://myNamespace.com" xmlns:p3="http://www.w3.org/2000/xmlns">
当然,我更希望 myns 的 xmlns 位于根 rss 节点而不是通道上,但我可以接受它在频道上。不幸的是,联合项目 xml 看起来像:
<item>
...
<a10:content type="text/xml">
<metadata xmlns="http://myNamespace.com">
<meta1>value1</meta2>
<meta2>value2</meta2>
</metadata>
</a10:content>
<myExtensions xmlns="http://myNamespace.com">
<ext1>value1</ext1>
<ext2>value2</ext2>
</myExtensions>
</item>
当然,我更希望看到的是:
<item>
...
<a10:content type="text/xml">
<myns:metadata>
<meta1>value1</meta2>
<meta2>value2</meta2>
</myns:metadata>
</a10:content>
<myns:myExtensions>
<ext1>value1</ext1>
<ext2>value2</ext2>
</myns:myExtensions>
</item>
是否有某种特殊的方法将 SyndicateFeed.AttributeExtensions 定义的命名空间与扩展 SyndicateItem 时使用的命名空间链接起来?
I am using the .NET SyndicationFeed class and have added some of my own extensions using SyndicationItem.ElementExtensions.Add() as well as setting SyndicationItem.Content to some Xml content.
My problem is that my namespace shows up multiple times in the XML output. Ideally I would apply a xmlns attribute to the root node and use its alias throughout the document.
I have seen examples that discuss using SyndicationFeed.AttributeExtensions as seen here. For example:
feed.AttributeExtensions.Add(
new System.Xml.XmlQualifiedName("myns", "http://www.w3.org/2000/xmlns"),
"http://myNamespace.com");
But, none of these examples show how to utilize the namespace later. For example, here are two ways I extend the feed:
XNamespace myNs = "http://myNamespace.com";
SyndicationItem item = new SyndicationItem();
XElement myMetadata = new XElement(myNs + "metadata");
myMetadata.Add(new XElement(myNs + "meta1", "value1"));
myMetadata.Add(new XElement(myNs + "meta2", "value2"));
item.Content = SyndicationContent.CreateXmlContent(myMetadata);
XElement myExtensions = new XElement(myNs + "myExtensions");
myExtensions.Add(new XElement(myNs + "ext1", "value1"));
myExtensions.Add(new XElement(myNs + "ext2", "value2"));
item.ElementExtensions.Add(myExtensions);
Hopefully I'm missing something simple. With the AttribuetExtensions.Add() method further above, my feed has the following for the initial XML:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel p3:myns="http://myNamespace.com" xmlns:p3="http://www.w3.org/2000/xmlns">
Granted, I'd prefer that the xmlns for myns be on the root rss node and not the channel, but I can live with it being on the channel. Unfortunately, the syndication item xml looks like:
<item>
...
<a10:content type="text/xml">
<metadata xmlns="http://myNamespace.com">
<meta1>value1</meta2>
<meta2>value2</meta2>
</metadata>
</a10:content>
<myExtensions xmlns="http://myNamespace.com">
<ext1>value1</ext1>
<ext2>value2</ext2>
</myExtensions>
</item>
Of course, what I'd prefer to see is:
<item>
...
<a10:content type="text/xml">
<myns:metadata>
<meta1>value1</meta2>
<meta2>value2</meta2>
</myns:metadata>
</a10:content>
<myns:myExtensions>
<ext1>value1</ext1>
<ext2>value2</ext2>
</myns:myExtensions>
</item>
Is there some special way of linking the namespace defined by SyndicationFeed.AttributeExtensions with that used when extending a SyndicationItem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要像这样为提要声明命名空间:
然后您可以声明您的 XNamespace 并在元素创建中使用它。
我在这里为 rdf 元素创建了一个,但您可以为任何自定义类型执行此操作。您甚至可以将命名空间的创建添加到 SyndicateFeed 和 SyndicateItem 的扩展方法中:
You may want to decalre the namespace for the feed like so:
Then you can declare your XNamespace and use it in your element creation.
I have created one here for an rdf element but you can do it for any of your custom types. You could even add the creation of your namespaces into an extension method of the SyndicationFeed and SyndicationItem: