Force XmlSerializer.Deserilize to ignore empty XML tags
-
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
-
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
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
-
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
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
-
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
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
-
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
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