使用 C# 进行原子输入

发布于 2024-09-29 23:17:53 字数 1355 浏览 1 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有你 2024-10-06 23:17:53

根据 XML 架构:

如果您只为 minOccurs 属性指定一个值,请确保该值小于或等于 maxOccurs 的默认值,即它为 0 或 1。同样,如果您只为 maxOccurs 属性指定一个值,它必须大于或等于minOccurs的默认值,即1或更多。 如果两个属性都被省略,则该元素必须恰好出现一次

                    <xs:element name="id" type="atom:idType" minOccurs="1" maxOccurs="1" />
                    <xs:element name="updated" type="atom:dateTimeType" minOccurs="1" maxOccurs="1" />

idatom:textType 。这是 textType 架构的片段:

<xs:complexType name="textType" mixed="true">
    <xs:annotation>
        <xs:documentation>
            The Atom text construct is defined in section 3.1 of the format spec.
        </xs:documentation>
    </xs:annotation>
    <xs:sequence>
        <xs:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="type" >
        <xs:simpleType>
            <xs:restriction base="xs:token">
                <xs:enumeration value="text"/>
                <xs:enumeration value="html"/>
                <xs:enumeration value="xhtml"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attributeGroup ref="atom:commonAttributes"/>
</xs:complexType>

如您所见,id 和更新的元素是强制性的,省略它们是非法的

另一方面,类型是可选的,因为 Use 的默认值是可选的。但我不知道如何隐藏它。

According the XML schema:

Be sure that if you specify a value for only the minOccurs attribute, it is less than or equal to the default value of maxOccurs, i.e. it is 0 or 1. Similarly, if you specify a value for only the maxOccurs attribute, it must be greater than or equal to the default value of minOccurs, i.e. 1 or more. If both attributes are omitted, the element must appear exactly once.

                    <xs:element name="id" type="atom:idType" minOccurs="1" maxOccurs="1" />
                    <xs:element name="updated" type="atom:dateTimeType" minOccurs="1" maxOccurs="1" />

id is atom:textType .Here is snippet of the schema for textType:

<xs:complexType name="textType" mixed="true">
    <xs:annotation>
        <xs:documentation>
            The Atom text construct is defined in section 3.1 of the format spec.
        </xs:documentation>
    </xs:annotation>
    <xs:sequence>
        <xs:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="type" >
        <xs:simpleType>
            <xs:restriction base="xs:token">
                <xs:enumeration value="text"/>
                <xs:enumeration value="html"/>
                <xs:enumeration value="xhtml"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attributeGroup ref="atom:commonAttributes"/>
</xs:complexType>

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.

季末如歌 2024-10-06 23:17:53

为什么要自己做?

使用 .Net 中的内置功能:

或 Argotic Syndicate 工具包:

编辑

抱歉,错过了您使用联合项目的部分。无论如何,这里是来自 ATOM 规范(RFC4287 第 4.1.2 节)的一些文本:

  • atom:entry 元素 必须 恰好包含一个atom:id 元素
  • atom:entry 元素必须 恰好包含一个一个原子:更新的元素

换句话说:如果删除这些项目,您将违反标准。

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

  • atom:entry elements MUST contain exactly one atom:id element
  • atom:entry elements MUST contain exactly one atom:updated element

In other words: You'll break the standard if you remove those items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文