.NET Framework Bookmark and Share   
 index > .NET Base Class Library > C# XML Deserialize, not working
 

C# XML Deserialize, not working

XML String toDeserialize:

<?xml version="1.0" encoding="utf-8"?>
<SimpleAuction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <id>604</id>
  <closed>false</closed>
  <suspended>false</suspended>
  <pvp>149.90</pvp>
  <finalPrice>0 â‚?span style="color:Blue;"></finalPrice>
  <currentPrice>1,72 â‚?span style="color:Blue;"></currentPrice>
  <currentTime>0:00:35</currentTime>
  <productName>Iomega externo 2TB</productName>
  <currentUser>vleonor</currentUser>
  <imageUrl>~/Images/63/1.jpg</imageUrl>
  <ccsClass>normal</ccsClass>
  <closeDate>0001-01-01T00:00:00</closeDate>
  <vTwingleBidder>false</vTwingleBidder>
  <vSuspend>true</vSuspend>
</SimpleAuction>


Heres my Class:

    [XmlRoot(ElementName = "SimpleAuction", IsNullable = false)]
    public sealed class TwingleItem
    {
        public TwingleItem(int autionID)
        {
            ID = autionID;
        }
        [XmlAttribute("id")]
        public int ID { get; set; }
        [XmlAttribute("closed")]
        public bool Closed { get; set; }
        [XmlAttribute("suspended")]
        public bool Suspended { get; set; }
        [XmlAttribute("pvp")]
        public decimal PVP { get; set; }
        [XmlAttribute("finalPrice")]
        public string FinalPrice { get; set; }
        [XmlAttribute("currentPrice")]
        public string CurrentPrice { get; set; }
        [XmlAttribute("currentTime")]
        public TimeSpan CurrentTime { get; set; }
        [XmlAttribute("productName")]
        public string ProductName { get; set; }
        [XmlAttribute("currentUser")]
        public string CurrentUser { get; set; }
        [XmlAttribute("imageUrl")]
        public string ImageUrl { get; set; }
        [XmlAttribute("ccsClass")]
        public string CssClass { get; set; }
        [XmlAttribute("closeDate")]
        public DateTime closeDate { get; set; }
        [XmlAttribute("vTwingleBidder")]
        public bool vTwingleBidder { get; set; }
        [XmlAttribute("vSuspend")]
        public bool vSuspend { get; set; }

        /// <summary>

        /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.

        /// </summary>

        /// <param name="characters">Unicode Byte Array to be converted to String</param>

        /// <returns>String converted from Unicode Byte Array</returns>

        public static String UTF8ByteArrayToString(Byte[] characters)
        {
            var encoding = new UTF8Encoding();
            var constructedString = encoding.GetString(characters);
            return (constructedString);
        }

        /// <summary>

        /// Converts the String to UTF8 Byte array and is used in De serialization

        /// </summary>

        /// <param name="pXmlString"></param>

        /// <returns></returns>

        public static Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            var encoding = new UTF8Encoding();
            var byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }

        /// <summary>

        /// Method to reconstruct an Object from XML string

        /// </summary>

        /// <param name="pXmlizedString"></param>

        /// <returns></returns>

        public static T DeserializeObject<T>(String pXmlizedString)
        {
            var xs = new XmlSerializer(typeof(T));
            var memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
            //var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (T)xs.Deserialize(memoryStream);
        }
    }
How i use the code:

            // text = Contain XML String
            var deserializedProduct = TwingleItem.DeserializeObject<TwingleItem>(text);
            if (deserializedProduct == null) MessageBox.Show("Error");
            MessageBox.Show(deserializedProduct.ProductName);

Nothing happen with that, no exceptions, no errors
None of the two MessageBox appear

What im doing wrong?
Thanks

sn4k3
knive1
To make your code work you have to do all of the following:

1. Add a parameterless constructor.
2. Change the TimeSpan data type (string will work).
3. Change all XmlAttribute attributes to XmlElement attrbutes.
4. Remove the
xmlns
=
"
http://tempuri.org/
"
part (I don't know what it's supposed to mean but it's not syntactically correct).

Otherwise you'll get an exception. (I don't know why you're not actually seeing any exceptions -- perhaps you're running this code in a background thread?)
  • Marked As Answer byknive1 Tuesday, September 22, 2009 7:18 PM
  •  
Michal Burger
I'm getting two exceptions on the line

var xs = new XmlSerializer(typeof(T));

(TwingleItem must have a parameterless constructor and type TimeSpan cannot be serialized.) How come you don't see them?
Michal Burger
I'm getting two exceptions on the line

var xs = new XmlSerializer(typeof(T));

(TwingleItem must have a parameterless constructor and type TimeSpan cannot be serialized.) How come you don't see them?
yes i already implemented a parameterless constructor
i switched TimeSpan to string, but still not working, nothing happen like before

now you get those exceptions? and what they say?

Thanks

sn4k3
knive1
To make your code work you have to do all of the following:

1. Add a parameterless constructor.
2. Change the TimeSpan data type (string will work).
3. Change all XmlAttribute attributes to XmlElement attrbutes.
4. Remove the
xmlns
=
"
http://tempuri.org/
"
part (I don't know what it's supposed to mean but it's not syntactically correct).

Otherwise you'll get an exception. (I don't know why you're not actually seeing any exceptions -- perhaps you're running this code in a background thread?)
  • Marked As Answer byknive1 Tuesday, September 22, 2009 7:18 PM
  •  
Michal Burger
To make your code work you have to do all of the following:

1. Add a parameterless constructor.
2. Change the TimeSpan data type (string will work).
3. Change all XmlAttribute attributes to XmlElement attrbutes.
4. Remove the
xmlns
=
"
http://tempuri.org/
"
part (I don't know what it's supposed to mean but it's not syntactically correct).

Otherwise you'll get an exception. (I don't know why you're not actually seeing any exceptions -- perhaps you're running this code in a background thread?)
thanks a lot, removing xmlns="http://tempuri.org/" works, yes it was a bg thread
sn4k3
knive1

You can use google to search for other answers

Custom Search

More Threads

• GDI+ vertical text not work for Chinese string
• How should we choose between class & struct?
• Problem with SerialPort class
• XML signed with cert issued from Intermediate CA
• How to Calculate Standard Deviation through Webform???
• using C# to access Active Directory.... and pull all "active" users.
• IExtractImage throws an error if executed in separate thread AND FolderBrowserDialog.ShowDialog() is called right before?
• Get parameters list in a function
• Save and restore desktop icon locations?
• Can I use "Me" (or "this" in C#) inside a constructor? (small code snippet inside)