DeserializeObject with XmlCDataSection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • riccuk
    New Member
    • Jun 2010
    • 1

    DeserializeObject with XmlCDataSection

    Hi I have the following class
    Code:
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class PageFile
    {
        private string parentGuidField;
        private string nameField;
    
        public string ParentGuid
        {
            get
            {
                return this.parentGuidField;
            }
            set
            {
                this.parentGuidField = value;
            }
        }
        public XmlCDataSection Name
        {
            get
            {    
                XmlDataDocument doc = new XmlDataDocument();
                XmlCDataSection cd = doc.CreateCDataSection(this.nameField);
                return cd;
            }
            set
            {        
                this.nameField = value.Value;
            }
        }
    }
    I am wanting to deserialize an xml string using the following generic deserializing class:
    Code:
    public static T DeserializeObject<T>(string XML)
    {
           using (StringReader stringreader = new StringReader(XML))
           {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                 return (T)serializer.Deserialize(stringreader);
            }
    }
    using :

    DeserializeObje ct<PageFile>(my XmlString);

    When I do this I get the following error:

    "Unable to cast object of type 'System.Xml.Xml Text' to type 'System.Xml.Xml CDataSection'."

    If "Name" was a string everything works fine. The problem is I have to have "Name" as a XmlCDataSection , how do I Deserialize my PageFile class using the DeserializeObje ct method that I use else where in my project?

    Many thanks
Working...