English 中文(简体)
明确定义使用pocos的Xml序列化
原标题:Declaratively define xml serialization using pocos

Normally in C# Xml types are marked with attributes to define the way how they get serialized:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace=
"urn:xmlns:25hoursaday-com:my-bookshelf")]
public class bookType {

    /// <remarks/>
    public string title;

    /// <remarks/>
    public string author;

   /// <remarks/>
   [System.Xml.Serialization.XmlElementAttribute("publication-date", 
DataType="date")]      
    public System.DateTime publicationdate;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string publisher;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute("on-loan")]
    public string onloan;
}

现在,我想在没有这些属性的情况下使用POCOS,即我有可能重复使用OR-Mapping,例如。 那么,在不改变序列化类型的情况下确定序列化方式是明智的。

问题在于:是否有办法确定某种类型的序列化方式,例如:测绘Xml文档。

最佳回答

是:

    XmlAttributeOverrides attribs = new XmlAttributeOverrides();
    attribs.Add(typeof(bookType), new XmlAttributes
    {
        XmlType = new XmlTypeAttribute { Namespace = "urn:xmlns:25hoursaday-com:my-bookshelf" },
    });
    attribs.Add(typeof(bookType), "publicationdate", new XmlAttributes
    {
        XmlElements = { new XmlElementAttribute("publication-date") { DataType = "date" } }
    });
    attribs.Add(typeof(bookType), "publisher", new XmlAttributes
    {
        XmlAttribute = new XmlAttributeAttribute()
    });
    attribs.Add(typeof(bookType), "onloan", new XmlAttributes
    {
        XmlAttribute = new XmlAttributeAttribute("on-loan")
    });

随后序号如下:

    XmlSerializer s = new XmlSerializer(typeof(bookType), attribs);
    var obj = new bookType { title = "a", author = "b",
        publicationdate = DateTime.Now, publisher = "c", onloan = "d"};
    s.Serialize(Console.Out, obj);

HOWEVER

and I can t caution this strongly enough; you must cache and re-use the XmlSerializer objects created in this way, as each one creates a dynamic serialization assembly that cannot be unloaded. If you don t cache and re-use, you will swamp the memory.

问题回答

暂无回答




相关问题
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. ...

热门标签