c# - Does XElement have built in support for nil=true -


i have following xml parsed xelement named entry.

<person>   <name>ann</name>   <age i:nil="true" xmlns:i="http://www.w3.org/2001/xmlschema-instance" /> </person> 

when fetching age property write this:

        var entry =             xelement.parse(                 "<person><name>ann</name><age i:nil=\"true\" xmlns:i=\"http://www.w3.org/2001/xmlschema-instance\" /></person>");         var age = entry.element("age").value; 

age "", , wonder if there kind of build in way null instead of ""?

most searches talk if entry isn't in xml, have nulls filled out this.

no, don't believe there's this, dead easy write extension method:

private static readonly xnamespace ns = "http://www.w3.org/2001/xmlschema-instance";  public static string nilawarevalue(this xelement element) {     xattribute nil = element.attribute(ns + "nil");     return nil != null && (bool) nil ? null : element.value; } 

or using nullable bool conversion:

public static string nilawarevalue(this xelement element) {     return (bool?) element.attribute(ns + "nil") ?? false ? null : element.value; } 

Comments