Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 24, 2013 @ 14:22
    Jeroen Breuer
    0

    Parseable xml with usercontrolwrapper not working

    Hello,

    I working on a custom datatype. For now I'm only trying to store a custom object in the xml, but it's not stored as real xml. Here is the code:

    VoteFormViewer.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VoteFormViewer.ascx.cs" Inherits="WebApplication.DataTypes.VoteFormViewer.VoteFormViewer" %>
    <asp:HiddenField ID="HiddenVoteValue" runat="server" />
    Show values later...

    VoteFormViewer.ascx.cs

    public partial class VoteFormViewer : UserControl, IUsercontrolDataEditor
    {
        #region Properties
    
        #region IUsercontrolDataEditor Members
    
        public object value
        {
            get
            {
                return SerializationHelper.ValueToXmlString(HiddenVoteValue.Value);
            }
            set
            {
                Vote vote = new Vote();
    
                HiddenVoteValue.Value = SerializationHelper.ValueToXmlString(vote);
            }
        }
    
        #endregion
    
        #endregion
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }

    Here is the test class I'm trying to serialize in xml:

    public class Vote
    {
        public int TestVal1 {get; set;}
        public int TestVal2 { get; set; }
        public int TestVal3 { get; set; }
    }

    This is the xml I'm getting in the Umbraco.config:

    <voteFormViewer>
      <string>&lt;?xml version="1.0"?&gt;&lt;Vote xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;&lt;TestVal1&gt;0&lt;/TestVal1&gt;&lt;TestVal2&gt;0&lt;/TestVal2&gt;&lt;TestVal3&gt;0&lt;/TestVal3&gt;&lt;/Vote&gt;</string>
    </voteFormViewer>

    If I try to deserialize it again I'm getting an error because it's not real xml. Any idea what goes wrong here? I'm using Umbraco 4.7.2. I looked at the value which is returned from HiddenVoteValue.Value, but it's not encoded or something. 

    Jeroen

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jun 24, 2013 @ 14:26
    Dirk De Grave
    0

    Most probably because of ntext vs nvarchar storage of your datatype value! Have had this before...

     

    /Dirk

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 24, 2013 @ 14:28
    Jeroen Breuer
    0

    I'm storing it as ntext because nvarch is too short.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 24, 2013 @ 14:31
    Jeroen Breuer
    0

    I've been using this blog as a reference: http://www.nibble.be/?p=100

    Jeroen

  • Comment author was deleted

    Jun 24, 2013 @ 14:33

    Hey Jeroen,

    in your getter you are serializing the HiddenVoteValue.Value and that will be a string... you should feed it an object of type vote

    get
           
    {
               
    returnSerializationHelper.ValueToXmlString(HiddenVoteValue.Value);
           
    }

     

     

  • Comment author was deleted

    Jun 24, 2013 @ 14:35

    In the example on my blog the customControl.Value is of type List<string>

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 24, 2013 @ 14:40
    Jeroen Breuer
    0

    Hi Tim,

    I marked your post as the solution because it pointed me in the right direction. This is my code now:

    public partial class VoteFormViewer : BaseUserControl, IUsercontrolDataEditor
    {
        #region Properties
    
        #region IUsercontrolDataEditor Members
    
        public object value
        {
            get
            {
                return ViewState["VoteValue"];
            }
            set
            {
                Vote vote = new Vote();
    
                ViewState["VoteValue"] = SerializationHelper.ValueToXmlString(vote);
            }
        }
    
        #endregion
    
        #endregion
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    }

    Because the HiddenVoteValue.Value (or ViewState which I'm using now) is already a string I don't need to use the SerializationHelper.ValueToXmlString. Thanks for helping!

    Jeroen

  • Comment author was deleted

    Jun 24, 2013 @ 14:40

    So should be more like

    public class Vote
        {
            public int TestVal1 { get; set; }
            public int TestVal2 { get; set; }
            public int TestVal3 { get; set; }
        }
     
        public partial class MultipleValuesDatatype : System.Web.UI.UserControl
            , umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    /// do something with vote
                }
            }
     
            private Vote vote;
     
            public object value
            {
                get
                {
                    //get the object value( just creating a new one now)
                    Vote v = new Vote();
     
                    return SerializationHelper.ValueToXmlString(v);
                }
                set
                {
                    if (value != null && string.IsNullOrEmpty(value.ToString()))
                        vote = new Vote();
                    else
                        vote = (Vote)SerializationHelper.ValueFromXmlString(value, typeof(Vote));
                }
            }
        }

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 24, 2013 @ 14:44
    Jeroen Breuer
    0

    A little more info about this might be useful. I'm tryin to create a datatype which only shows data in the datatype with the option to export. So in the set I need to do some stuff to display the data and in the get only return that same data again because it can't be changed in Umbraco. That's why the viewstate is fine. The data will be changed on the front of the website with the Umbraco Document api. 

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft