使用 C# 进行原子输入
如何使用 C# 和 .NET 4 创建 Atom 条目?
我需要使用此结构创建一个条目:
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:f="XXX:aaa">
<title>title1</title>
<summary>summary1</summary>
</entry>
我尝试使用 SyndicateItem 类执行此操作,但条目包含的信息比我需要的更多:
SyndicationItem atom = new SyndicationItem();
atom.Title = new TextSyndicationContent("test1", TextSyndicationContentKind.Plaintext);
atom.Summary = new TextSyndicationContent("summary1");
atom.AttributeExtensions.Add(new XmlQualifiedName("f", "http://www.w3.org/2000/xmlns/"), "XXX:aaa");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = " ";
settings.NewLineOnAttributes = true;
StringBuilder sb = new StringBuilder();
XmlWriter xml = XmlWriter.Create(sb,settings);
atom.SaveAsAtom10(xml);
xml.Close();
Console.WriteLine(sb.ToString());
结果是:
<entry xmlns:f="XXX:aaa" xmlns="http://www.w3.org/2005/Atom">
<id>uuid:34381971-9feb-4444-9e6a-3fbc412ac6d2;id=1</id>
<title type="text">title1</title>
<summary type="text">summary1</summary>
<updated>2010-10-29T14:02:48Z</updated>
</entry>
如何在没有 , 和 type="*" 的情况下创建原子条目对象来制作它看起来正是我想要的吗?
你能帮我简化代码吗?
谢谢!
How can I make an Atom entry with C# and .NET 4 ?
I need to make an entry with this structure:
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:f="XXX:aaa">
<title>title1</title>
<summary>summary1</summary>
</entry>
I tried to do this with SyndicationItem class but entry contains more info than I need:
SyndicationItem atom = new SyndicationItem();
atom.Title = new TextSyndicationContent("test1", TextSyndicationContentKind.Plaintext);
atom.Summary = new TextSyndicationContent("summary1");
atom.AttributeExtensions.Add(new XmlQualifiedName("f", "http://www.w3.org/2000/xmlns/"), "XXX:aaa");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = " ";
settings.NewLineOnAttributes = true;
StringBuilder sb = new StringBuilder();
XmlWriter xml = XmlWriter.Create(sb,settings);
atom.SaveAsAtom10(xml);
xml.Close();
Console.WriteLine(sb.ToString());
And the result is:
<entry xmlns:f="XXX:aaa" xmlns="http://www.w3.org/2005/Atom">
<id>uuid:34381971-9feb-4444-9e6a-3fbc412ac6d2;id=1</id>
<title type="text">title1</title>
<summary type="text">summary1</summary>
<updated>2010-10-29T14:02:48Z</updated>
</entry>
How can I create atom entry object without , and type="*" to make it look exactly I want?
Can you help me to simplify the code?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 XML 架构:
id
是atom:textType
。这是textType
架构的片段:如您所见,id 和更新的元素是强制性的,省略它们是非法的。
另一方面,类型是可选的,因为
Use
的默认值是可选的。但我不知道如何隐藏它。According the XML schema:
id
isatom:textType
.Here is snippet of the schema fortextType
:So as you can see, id and updated elements are mandatory and is illegal to omit them.
On the other hand, type is optional since default value for the
Use
is optional. But I dont know a way to hide it.为什么要自己做?
使用 .Net 中的内置功能:
或 Argotic Syndicate 工具包:
编辑
抱歉,错过了您使用联合项目的部分。无论如何,这里是来自 ATOM 规范(RFC4287 第 4.1.2 节)的一些文本:
换句话说:如果删除这些项目,您将违反标准。
Why do it yourself?
Either use the built in features in .Net:
Or the Argotic Syndication toolkit:
Edit
Sorry, missed the part where you use syndication item. Anyway here is some text from the ATOM specification (RFC4287 Section 4.1.2):
In other words: You'll break the standard if you remove those items.