English 中文(简体)
Atom entry with C#
原标题:

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!

最佳回答

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.

问题回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签