2010-10-29 125 views
5

如何使用C#和.NET 4創建Atom條目?C原子條目#

我需要這個結構中的條目:

<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 =「*」來使它看起來完全一樣想要什麼?

你能幫我簡化代碼嗎?

謝謝!

回答

1

爲什麼自己做?

或者使用內置的功能在.net:

還是Argotic的整合工具包:

編輯

對不起,錯過了您使用聯合項目的部分。無論如何,這裏是來自ATOM規範(RFC4287第4.1節)的一些文本。2):

  • 原子:entry元素MUST包含正好一個原子:id元素
  • 原子:entry元素MUST包含正好一個原子:更新元件

換句話說:如果您刪除這些項目,您將破壞標準。

+0

這正是我想要做的,我使用syndicationitem對象,但我不能讓它創建我想要的條目,但謝謝你的argotic我會嘗試。但是最好使用syndicationitem,因爲它已經在框架中。 – 2010-10-29 18:39:02

+0

我誤解了你的問題,我的答案已更新。 – jgauffin 2010-10-29 19:21:32

+0

謝謝你的幫助! – 2010-10-29 19:53:05

2

根據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的默認值是可選的。但我不知道一種方法來隱藏它。

+0

好吧,似乎SyndicationItem生成正確的原子條目。謝謝!我怎樣才能簡化代碼並刪除字段? – 2010-10-29 14:41:57

+0

我有更新。對不起,我錯了,屬性是可選的。 – Aliostad 2010-10-29 15:21:27

+0

可能有SyndicationItem將其刪除的選項? – 2010-10-29 15:32:16