views:

62

answers:

2
+3  Q: 

Atom entry with C#

Hello, 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"&gt;
  <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!

+2  A: 

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.

Aliostad
Ok, it seems that SyndicationItem generates right atom entry. Thanks! And how can I simplify the code and remove <id> and <updated> fields?
Luberg
I have update. Sorry I was wrong, the attribute was optional.
Aliostad
May be there is an option for SyndicationItem to remove it?
Luberg
+1  A: 

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.

jgauffin
This is exactly what am I trying to do, I use syndicationitem object, but I can't make it to create the entry I want, but thanks for argotic I'll try that. But it is better to use syndicationitem because it is already in the framework.
Luberg
I misread your question, my answer is updated.
jgauffin
Thank you for your help!
Luberg