5

C# と .NET 4 で Atom エントリを作成するにはどうすればよいですか?

この構造でエントリを作成する必要があります。

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:f="XXX:aaa">
  <title>title1</title>
  <summary>summary1</summary>
</entry>

SyndicationItem クラスでこれを実行しようとしましたが、エントリには必要以上の情報が含まれています。

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="*" を使用せずにアトムエントリオブジェクトを作成して、希望どおりに見えるようにするにはどうすればよいですか?

コードを単純化するのを手伝ってもらえますか?

ありがとう!

4

2 に答える 2

2

XMLスキーマによると:

minOccurs属性のみに値を指定する場合は、maxOccursのデフォルト値以下、つまり0または1であることを確認してください。同様に、maxOccurs属性のみに値を指定する場合は、次のようにする必要があります。 minOccursのデフォルト値以上、つまり1以上。両方の属性を省略した場合、要素は1回だけ表示される必要があります

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

idはです。これは、次atom: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要素とupdated要素は必須であり、それらを省略することは違法です。

一方、のデフォルト値はオプションであるため、typeはオプションUseです。しかし、私はそれを隠す方法を知りません。

于 2010-10-29T14:21:21.083 に答える
1

なぜ自分でやるのですか?

.Net の組み込み機能を使用します。

または Argotic Syndication ツールキット:

編集

申し訳ありませんが、シンジケーション アイテムを使用する部分を見逃しました。とにかく、ここに ATOM 仕様 (RFC4287 セクション 4.1.2) からのテキストがあります。

  • atom:entry要素にはatom:id要素が1つだけ含まれている必要があります
  • atom:entry 要素には、atom:updated 要素が 1 つだけ含まれている必要があります。

つまり、これらの項目を削除すると、基準を破ることになります。

于 2010-10-29T18:33:43.007 に答える