Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Force XmlSerializer.Deserilize to ignore empty XML tags

Force XmlSerializer.Deserilize to ignore empty XML tags

Scheduled Pinned Locked Moved C#
questionrubydatabasexml
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    I'm having trouble deserilizing an xml node that has empty tags. See XML: 2007-08-24T16:15:16-07:00 this campaign has alerady been sent 1 4 complete_campaign this campaign is read to send This XML is generated from a rails web service and respresents a table in the database. When I try to deserilize this the deserlizer fails on the empty tags. How can I make the deserializer simply ignore these tags as they have no value anyways...? /\ |_ E X E GG

    T 1 Reply Last reply
    0
    • E eggie5

      I'm having trouble deserilizing an xml node that has empty tags. See XML: 2007-08-24T16:15:16-07:00 this campaign has alerady been sent 1 4 complete_campaign this campaign is read to send This XML is generated from a rails web service and respresents a table in the database. When I try to deserilize this the deserlizer fails on the empty tags. How can I make the deserializer simply ignore these tags as they have no value anyways...? /\ |_ E X E GG

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      Check out this[^] for elements and this[^] for attributes. Basically, you are going to read them in, but you can trash them later. Or you could use this[^] to ignore those elements/attributes.

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      E 1 Reply Last reply
      0
      • T TJoe

        Check out this[^] for elements and this[^] for attributes. Basically, you are going to read them in, but you can trash them later. Or you could use this[^] to ignore those elements/attributes.

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        E Offline
        E Offline
        eggie5
        wrote on last edited by
        #3

        Sorry, But I'm a little overwhelmed. Maybe you can help me more if I'm more specific. I have this XML that I'm trying to serilize into a ContentCampaign obj.: 0 serilizatoin test 4 i am subject i am body 0001-01-01T00:00:00 4 0001-01-01 0 0001-01-01T00:00:00 0001-01-01T00:00:00 It works find until a field node is empty. For example . This xml is generated from a database table and when a value is null it simple generates an empty xml node. The problem with the empty ad_id_source node is tha now, when the deserializer runs, it throws an exception. Simply, is there a way for the deserializer to ignore these empty nodes? I noticed that if a node that maps to a string is empty is just defaults to null. Can I configure the deserilizer to set any empty nodes respective type to it's default value? eg. 0 for int, 0001-01-01 for DateTimes? -- modified at 19:59 Tuesday 2nd October, 2007 /\ |_ E X E GG

        T 1 Reply Last reply
        0
        • E eggie5

          Sorry, But I'm a little overwhelmed. Maybe you can help me more if I'm more specific. I have this XML that I'm trying to serilize into a ContentCampaign obj.: 0 serilizatoin test 4 i am subject i am body 0001-01-01T00:00:00 4 0001-01-01 0 0001-01-01T00:00:00 0001-01-01T00:00:00 It works find until a field node is empty. For example . This xml is generated from a database table and when a value is null it simple generates an empty xml node. The problem with the empty ad_id_source node is tha now, when the deserializer runs, it throws an exception. Simply, is there a way for the deserializer to ignore these empty nodes? I noticed that if a node that maps to a string is empty is just defaults to null. Can I configure the deserilizer to set any empty nodes respective type to it's default value? eg. 0 for int, 0001-01-01 for DateTimes? -- modified at 19:59 Tuesday 2nd October, 2007 /\ |_ E X E GG

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          I'm sorry, I misunderstand what you wanted. I though you had extraneous elements (e.g. items without a matching property in your C# class) that you wanted to skip. You could derive your own class from XmlTextReader[^]and override the Read[^] method. But that would get messy because you have to take into account elements that only have sub-elements. The easiest way is to probably define another property that you use for serialization like so:

          private Int32 myInt = 0;
          [XmlIgnore]
          public Int32 MyInt {
          get {
          return this.myInt;
          }
          set {
          this.myInt = value;
          }
          }

          [XmlElement("MyInt")]
          public String MyIntSerialize {
          get {
          return this.MyInt.ToString();
          }
          set {
          Int32 newValue = 42; // default for blank elements
          if (!String.IsNullOrEmpty(value))
          newValue = Int32.Parse(value);
          this.myInt = newValue;
          }
          }

          This gets messy also because you have to double up on your properties.

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          E 1 Reply Last reply
          0
          • T TJoe

            I'm sorry, I misunderstand what you wanted. I though you had extraneous elements (e.g. items without a matching property in your C# class) that you wanted to skip. You could derive your own class from XmlTextReader[^]and override the Read[^] method. But that would get messy because you have to take into account elements that only have sub-elements. The easiest way is to probably define another property that you use for serialization like so:

            private Int32 myInt = 0;
            [XmlIgnore]
            public Int32 MyInt {
            get {
            return this.myInt;
            }
            set {
            this.myInt = value;
            }
            }

            [XmlElement("MyInt")]
            public String MyIntSerialize {
            get {
            return this.MyInt.ToString();
            }
            set {
            Int32 newValue = 42; // default for blank elements
            if (!String.IsNullOrEmpty(value))
            newValue = Int32.Parse(value);
            this.myInt = newValue;
            }
            }

            This gets messy also because you have to double up on your properties.

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            E Offline
            E Offline
            eggie5
            wrote on last edited by
            #5

            Hi, I've just decided to impliment a support layer that removes empty nodes. This is working now and I haven't found any issues with it:

            string ct = this.HttpWebResponse.Headers["Content-Type"];
            if (ct.Contains("application/xml"))
            this.response_text = FilterOutEmptyNodes(this.response_text);
            }

                private string FilterOutEmptyNodes(string p)
                {
                    //run regex to remove empty Ruby/Rails ActiveRecord nodes for C#/Asp.net compatability
                    return System.Text.RegularExpressions.Regex.Replace(p, "<\[^>\]\*><\[^>\]\*>", "");
                }
            

            /\ |_ E X E GG

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups