Dev Team Assemble

Evil beware!
Add to Technorati Favorites

Archive

Tag: xml

I have been working with xml serialization over the last week or so and ran into a strange issue today. It only came to light when I decided to implement IXmlSerializable and write my own methods for reading and writing the xml. I posted recently about using he XmlRootAttribute as a fix for the xmlns='' > was not expected error I was seeing. This all worked fine and dandy until I really started to utilize that method over and over again in my code. I found the performance of this to be horrible...so horrible in fact it was taking 2-3 minutes to deserialize a moderately complex 1MB xml file. After doing some digging (on "The Google") I discovered that microsoft decided to only implement caching on the Xml serializer when you use one of the two "more common" constructors (see documentation). Of course it would seem the one I need would not be one of them.

I was thinking of somehow adding the XmlSerializers for each type/root pair that I was using to the application level cache or something like that but thought that might bring with it its own set of challenges. I found a great article here on stack overflow that shows a possible workaround for the problem using a static class to act as a cache or you.

Please note this code is taken directly from the article above:


public static class XmlSerializerCache
{
private static readonly Dictionary cache =
new Dictionary();

public static XmlSerializer Create(Type type, XmlRootAttribute root)
{
var key = String.Format( CultureInfo.InvariantCulture, "{0}:{1}", type, root.ElementName);

if (!cache.ContainsKey(key))
{
cache.Add(key, new XmlSerializer(type, root));
}

return cache[key];
}
}

Here is how you use it to create an XmlSerializer object:


var xmlRootAttribute = new XmlRootAttribute("ExampleElement");
var serializer = XmlSerializerCache.Create(target.GetType(), xmlRootAttribute);

After implementing that I was still experiencing the 2-3 minute deserialization one the first call but all subsequent calls to that code with similar xml took only 2 seconds.

NOTE:
Seems microsoft is in the habit of only half implementing caching in their code. This problem is almost identical to the problems with the CrossListQueryCache object in SharePoint....where they again only implemented caching for two of the four available method signatures. Here is an interesting article I found in the nether-regions of cyberspace where microsoft state this half implementation is by design...

Technorati Tags: , , , , , , ,

I had what turns out to be a fairly common error the other day when trying to deserialize an xml file in one of my applications.  Here is the code I was running:


XmlSerializer ser = new XmlSerializer(typeof(MyObject));
XmlReader xRdr = XmlReader.Create(new StringReader(xmlData));
MyObject tvd = (MyObject)ser.Deserialize(xRdr);

As it turns out this was causing the error: xmlns=''> was not expected during deserialization. Alot of the resources online (including the msdn article here) were pointing me to the namespaces not being the same on the serializer and document. I am not sure why it wasn't working for me because I was playing around with the namespaces but couldn't seem to figure out how to get that working. Maybe because I was only serializing a fragment of a document I'm not sure. Anyway it turns out that specifying the XmlRootAttribute in the XmlSerializer constructor fixed the problem for me. This Stack Overflow article really helped Here is the snippet of code that I am using now:


XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = elementName;
xRoot.IsNullable = true;

XmlSerializer ser = new XmlSerializer(typeof(MyObject), xRoot);
XmlReader xRdr = XmlReader.Create(new StringReader(xmlData));
MyObject tvd = (MyObject)ser.Deserialize(xRdr);

Hope this helps

Technorati Tags: , , , , ,

Sometimes when I am working with SharePoint I really feel like I got it.  You now that feeling you get when you know exactly what you are doing and have no problems implementing it...then there are times when I really just want to smash something.  And it really doesn't matter what...it could be anything!  This is the feeling I got in the last couple of days working with custom field types in SharePoint.

It seems there are lots of blogs and articles out there talking about how to do it, but from what I could see (and to be fair I did not read them all so if you know of some good posts that would be great) it felt like most of the time I was only getting half the story.  I just finished putting together a very basic Custom Field Type that will display a multi line textbox in edit/new mode and simply display the value in a literal control in display mode.  In the grand scheme of things I am building a base XML Field Type to use for a series of additional Custom field types so essentially I want to store a string (yes I could use the XmlDocument)...but alas thats a story for another day.

So a bit of a primer, or rehash so I don't forget, when developing custom field types there are many pieces to it....not all of them required.  Here is a list of the items and what they do:

  • Field Type class - this class acts as the controller and hooks the rendering control, field type xml definition and the value class together.  It also provides validation at the field level via the GetValidatedString() method of the SPField object
  • Field Control class - this is the rendering engine and gives you the power to display your control in any manner you wish as well as perform validation on the data being saved via the UI.
  • Field Value class - allows for custom logic to be put around mutiple column fields and different data structures that will be storing data for your field type.
  • .ascx file in CONTROLTEMPLATES directory - this supplies rendering markup via a user control and hooks into the field control class
  • fldtypes_* - this file that gets installed into the 12/TEMPLATES/XML directory and contains the information that SharePoint requires in order to load your field type into the system.  Such as the assembly to use, the parent type and other information.  This is required if the type is going to be applied to content types and become a field on a list.

So for my requirements I only needed the field type, the field control and the fldtypes_xxx.xml file.  Lets look at the field type class I built

public class XmlFieldType : SPField
   {
        public XmlFieldType(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }
        public XmlFieldType(SPFieldCollection fields, string fieldName, string displayName) : base(fields, fieldName, displayName) { }
 
        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                BaseFieldControl fldControl = new XmlFieldControl();
                fldControl.FieldName = InternalName;
                return fldControl;
            }
        }
 
        public override object GetFieldValue(string value)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            return value;
        }
 
        public override Type FieldValueType
        {
            get { return typeof(string); }
        }
 
        public override string DefaultValue
        {
            get
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(this.SchemaXml);
                XmlNode nodeInFieldSchema = doc.SelectSingleNode("Field/Default");
                if (nodeInFieldSchema != null)
                    return nodeInFieldSchema.InnerXml;
                return null;
            }
            set { base.DefaultValue = value; }
        }
 
        public override string GetValidatedString(object value)
        {
            base.GetValidatedString(value);
            if (value == null)
            {
                if (this.Required) throw new SPFieldValidationException("Invalid value for required field.");
                return string.Empty;
            }
            else
            {
                return value.ToString();
            }
        }
}

Lets review

  • The two constructors are required...don't even bother
  • FieldRenderingControl() method tells the field type which control to use
  • GetFieldValue() method returns the value of the field....if we were using a custom type you would cast that to the appropriate type here
  • FieldValueType() - get the type of the Value class
  • DefautValue() - this gets the default value from the field if one is defined
  • GetValidatedString() - perform field level validation

The Field Control Class is fairly simple as well:

public class XmlFieldControl : BaseFieldControl
{
        protected Literal _values;
        protected TextBox _editor;
 
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            EnsureChildControls();
        }
 
        protected override void CreateChildControls()
        {
 
            if (this.Field == null || this.ControlMode == SPControlMode.Invalid)
                return;
 
            Controls.Clear();
            base.CreateChildControls();
            _editor = new TextBox();
            _editor.TextMode = TextBoxMode.MultiLine;
            _editor.Height = new Unit(300);
            _editor.Width = new Unit(500);
            Controls.Add(_editor);
 
            if (ControlMode == SPControlMode.Display)
            {
                _values = new Literal();
                _values.Text = Convert.ToString(ItemFieldValue);
                Controls.Add(_values);
            }
        }
 
        protected override void Render(HtmlTextWriter output)
        {
 
            EnsureChildControls();
            if (ControlMode == SPControlMode.Display)
            {
                if (_values != null)
                    _values.RenderControl(output);
            }
            else
            {
                if (_editor != null)
                    _editor.RenderControl(output);
            }
        }
 
        public virtual string Text
        {
            get
            {
                this.EnsureChildControls();
                if (this._editor == null)
                {
                    return null;
                }
                return this._editor.Text;
            }
 
            set
            {
                this.EnsureChildControls();
                if (this._editor != null)
                {
                    this._editor.Text = value;
                }
            }
        }
 
        public override object Value
        {
            get
            { return this.Text; }
            set
            {
                if (base.Field != null)
                {
                    this.Text = base.Field.GetFieldValueForEdit(value);
                    base.Value = (string)value;
                    base.UpdateFieldValueInItem();
                }
            }
        }
    }

Lets take a look at what this class is doing:

  • OnInit() - come on, do I have to explain?
  • CreateChildControls() - initialize your controls and add to the Controls collection
  • Render() - writes the controls to the HtmlTextWriter
  • Text - This sets/gets the value in the controls
  • Value - this sets the value in the Text property and notice the base.UpdateFieldValueInItem(); statement...this oushes the value from Value to ItemFieldValue and allows you to access this value in display mode because Value returns null (which i did not find the documentation on M$ thank you very much).

So that leaves the last piece of the pie for my simple implementation the fldtypes file:

<FieldTypes>
<FieldType>
<Field Name="TypeName">XmlFieldType</Field>
<Field Name="ParentType"></Field>
<Field Name="InternalType">Note</Field>
<Field Name="SQLType">ntext</Field>
<Field Name="TypeDisplayName">Xml Field</Field>
<Field Name="TypeShortDescription">Xml controls</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowOnListCreate">TRUE</Field>
<Field Name="Sortable">FALSE</Field>
<Field Name="AllowBaseTypeRendering">FALSE</Field>
<Field Name="Filterable">FALSE</Field>
<Field Name="FieldTypeClass">Website.CustomFields.XmlFieldType, Website.CustomFields, Version=1.0.0.0, Culture=neutral, PublicKeyToken=346fcdd567259ee6</Field>
<RenderPattern Name="HeaderPattern">
<Property Select="DisplayName" HTMLEncode="TRUE"/>
</RenderPattern>
<RenderPattern Name="DisplayPattern">
<Column/>
</RenderPattern>
</FieldType>
</FieldTypes>

So there you have it.  All I need to do in order to inherit from this is to remove the rendering (which i dont need anyway  as this is going to be a base type) and it should be good to go.  Those three pieces of code can be installed into your SharePoint environment and should help you get started to understanding custom field types.

Don't forget to add the SafeControl entry, Adjust your trust level and move the .dll into your bin folder as well.

Download the code here

Resources I found useful:

  • I did work through the example in Andrew Connell's book (Professional SP 2007 WCM development) and that worked fine and he actually does a great job of explaining most of whats going on here.
  • MSDN...where else
  • Charlie Holland has a pretty good article series on this stuff but unfortunately I couldn't get it working.  I also wanted a simple bare bones implementation.
Sometimes when I am working with SharePoint I really feel like I got it.  You now that feeling you get when you know exactly what you are doing and have no problems implementing it...then there are times when I really just want to smash something.  And it really doesn't matter what...it could be anything!  This is the feeling I got in the last couple of days working with custom field types in SharePoint.  It seems there are lots of blogs and articles out there talking about how to do it, but from what I could see (and to be fair I did not read them all so if you know of some good posts that would be great) it felt like most of the time I was only getting half the story.  I just finished putting together a very basic Custom Field Type that will display a multiline textbox in edit/new mode and simply display the value in a literal control in display mode.  In the grand scheme of things I am building a base XML Field Type to use for a series of additional Custom field types so essentially I want to store a string (yes I could use the XmlDocument)...but alas thats a story for another day.

So a bit of a primer, or rehash so I don't forget, when developing custom field types there are many pieces to it....not all of them required.  Here is a lit of the items and what they do:

* Field Type class - this class acts as the controller and hooks the rendering control, field type xml definition and the value class together.  It also provides validation at the field level via the GetValidatedString() method of the SPField object
* Field Control class - this is the rendering engine and gives you the power to display your control in any manner you wish as well as perform validation on the data being saved via the UI.
* Field Value class - allows for custom logic to be put around mutiple column fields and different data structures that will be storing data for your field type.
* .ascx file in CONTROLTEMPALTES directory - this supplies rendering markup via a usercontrol and hooks into the field control class
* fldtypes_* - this file that gets installed into the 12/TEMPLATES/XML directory and contrains the information that SharePoint requires in order to load your field type into the system.  Such as the assembly to use, the parent type and other information.  This is required if the type is going to be applied to content types and become a field on a list.

So for my requirements I only needed the field type, the field control and the fldtypes_xxx.xml file.  Lets look at the field type class I built

public class XmlFieldType : SPField
{
public XmlFieldType(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }
public XmlFieldType(SPFieldCollection fields, string fieldName, string displayName) : base(fields, fieldName, displayName) { }
 
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fldControl = new XmlFieldControl();
fldControl.FieldName = InternalName;
return fldControl;
}
}
 
public override object GetFieldValue(string value)
{
if (string.IsNullOrEmpty(value))
return null;
return value;
}
 
public override Type FieldValueType
{
get { return typeof(string); }
}
 
public override string DefaultValue
{
get
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(this.SchemaXml);
XmlNode nodeInFieldSchema = doc.SelectSingleNode("Field/Default");
if (nodeInFieldSchema != null)
return nodeInFieldSchema.InnerXml;
return null;
}
set { base.DefaultValue = value; }
}
 
public override string GetValidatedString(object value)
{
base.GetValidatedString(value);
if (value == null)
{
if (this.Required) throw new SPFieldValidationException("Invalid value for required field.");
return string.Empty;
}
else
{
return value.ToString();
}
}
}

Lets review

* The two constructors are required
* FieldRenderingControl() method tells the field type which control to use
* GetFieldValue() method returns the value of the field....if we were using a custom type you would cast that to the appropriate type here
* FieldValueType() - get the type of the Value class
* DefautValue() - this gets the default value from the field if one is defined
* GetValidatedString() - perform field level validation

The Field Control Class is fairly simple as well:

public class XmlFieldControl : BaseFieldControl
{
protected Literal _values;
protected TextBox _editor;
 
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
EnsureChildControls();
}
 
protected override void CreateChildControls()
{
 
if (this.Field == null || this.ControlMode == SPControlMode.Invalid)
return;
 
Controls.Clear();
base.CreateChildControls();
_editor = new TextBox();
_editor.TextMode = TextBoxMode.MultiLine;
_editor.Height = new Unit(300);
_editor.Width = new Unit(500);
Controls.Add(_editor);
 
if (ControlMode == SPControlMode.Display)
{
_values = new Literal();
_values.Text = Convert.ToString(ItemFieldValue);
Controls.Add(_values);
}
}
 
protected override void Render(HtmlTextWriter output)
{
 
EnsureChildControls();
if (ControlMode == SPControlMode.Display)
{
if (_values != null)
_values.RenderControl(output);
}
else
{
if (_editor != null)
_editor.RenderControl(output);
}
}
 
public virtual string Text
{
get
{
this.EnsureChildControls();
if (this._editor == null)
{
return null;
}
return this._editor.Text;
}
 
set
{
this.EnsureChildControls();
if (this._editor != null)
{
this._editor.Text = value;
}
}
}
 
public override object Value
{
get
{ return this.Text; }
set
{
if (base.Field != null)
{
this.Text = base.Field.GetFieldValueForEdit(value);
base.Value = (string)value;
base.UpdateFieldValueInItem();
}
}
}
}

sdfsdf

    XmlFieldType
 
    Note
    ntext
    Xml Field
    Xml controls
    TRUE
    TRUE
    FALSE
    FALSE
    FALSE
    Website.CustomFields.XmlFieldType, Website.CustomFields, Version=1.0.0.0, Culture=neutral, PublicKeyToken=346fcdd567259ee6

I did work through the example in Andrew Connell's book (Professional SP 2007 WCM development) and that worked fine and he actually does a great job of explaining most of whats going on here.

http://msdn.microsoft.com/en-us/library/ms446361.aspx

Technorati Tags: , , , ,