views:

341

answers:

3

Trying to deserialize some xml snippits from a vendor into objects. The problem is that I'm getting an invalid format on every empy element tag. I can deserialize the object no problem when all of the elements have values. Or the empty elements are ommitted.

Xml Snippit:

<foo>
<propOne>1</propOne>
<propTwo />
</foo>

C# Class:

[Serialilbe()]
public class foo { public foo(){}
[XmlElementAttribute(IsNullable = true)]
public int? propOne {get;set;}
[XmlElementAttribute(IsNullable = true)]
public int? propTwo {get;set;}
}

Is there a setting on the class I can make to adjust the parsing?

or

Is there an easy way I can apply xsl to remove these elements?

or

Should I use regEx to remove the empty elements be fore desrializing?

or

an even better way?

+2  A: 

See this article: Can XmlSerializer deserialize into a Nullable?

In a nutshell your Xml should look like this if you want to use Nullable types:

<foo xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'&gt;
<propOne>1</propOne>
<propTwo xsi:nil='true'/>
</foo>

The two changes are adding the namespace, and explicitly setting xsi:nil to true on the null element.

If you don't have control over your Xml there is a more advanced technique described here: Using XmlSerializer to deserialize into a Nullable

Andrew Anderson
A: 

For simplicity, why don't you parse the xml explictly using XmlDocument and XPath? Use XPath to explictly access each xml node e.g.

XmlNode node = xml.SelectSingleNode ("foo/PropOne");
if (node != null)
{
     propOneValue = node.innerText;
}
Syd
A: 

The most uniform way to clean out these nodes appears to be to add a RegEx filter to the deserializer.

    public static T Deserialize<T>(string xml){
        XmlSerializer xs = new XmlSerializer(typeof(T));
        string cleanXml = Regex.Replace(xml, @"<[a-zA-Z].[^(><.)]+/>",
                                        new MatchEvaluator(RemoveText));
        MemoryStream memoryStream = new MemoryStream((new UTF8Encoding()).GetBytes(cleanXml));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        return (T)xs.Deserialize(memoryStream);
    }
  static string RemoveText(Match m) { return "";}