C# XElement是否内置了对nil=true的支持

C# XElement是否内置了对nil=true的支持,c#,xelement,C#,Xelement,我将以下xml解析为一个名为entry的XElement <Person> <Name>Ann</Name> <Age i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" /> </Person> 安 获取age属性时,我写下以下内容: var entry = XElement.Parse(

我将以下xml解析为一个名为entry的XElement

<Person>
  <Name>Ann</Name>
  <Age i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" />
</Person>
获取age属性时,我写下以下内容:

        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;
var条目=
解析(
“安”);
var age=条目。元素(“age”)。值;
年龄现在是“”,我想知道是否有某种构建方式可以将“”改为null


大多数搜索都会讨论条目是否不在xml中,但我总是这样填写空值。

不,我不相信有任何相关内容,但编写扩展方法非常简单:

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;
}
或使用可为空的布尔转换:

public static string NilAwareValue(this XElement element)
{
    return (bool?) element.Attribute(ns + "nil") ?? false ? null : element.Value;
}

不,我不相信有什么可以做到这一点,但编写一个扩展方法非常简单:

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;
}
或使用可为空的布尔转换:

public static string NilAwareValue(this XElement element)
{
    return (bool?) element.Attribute(ns + "nil") ?? false ? null : element.Value;
}

不,我不相信有什么可以做到这一点,但编写一个扩展方法非常简单:

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;
}
或使用可为空的布尔转换:

public static string NilAwareValue(this XElement element)
{
    return (bool?) element.Attribute(ns + "nil") ?? false ? null : element.Value;
}

不,我不相信有什么可以做到这一点,但编写一个扩展方法非常简单:

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;
}
或使用可为空的布尔转换:

public static string NilAwareValue(this XElement element)
{
    return (bool?) element.Attribute(ns + "nil") ?? false ? null : element.Value;
}