清理具有属性“nil=true”的 Xml 元素- 创建 linq 版本?

发布于 2025-01-04 15:59:45 字数 1121 浏览 0 评论 0原文

我正在尝试清理文档中带有属性“nil=true”的 Xml 元素。 我想出了这个算法,但我不喜欢它的外观。

有人知道这个算法的 linq 版本吗?

    /// <summary>
    /// Cleans the Xml element with the attribut "nil=true".
    /// </summary>
    /// <param name="value">The value.</param>
    public static void CleanNil(this XElement value)
    {
        List<XElement> toDelete = new List<XElement>();

        foreach (var element in value.DescendantsAndSelf())
        {
            if (element != null)
            {
                bool blnDeleteIt = false;
                foreach (var attribut in element.Attributes())
                {
                    if (attribut.Name.LocalName == "nil" && attribut.Value == "true")
                    {
                        blnDeleteIt = true;
                    }
                }
                if (blnDeleteIt)
                {
                    toDelete.Add(element);
                }
            }
        }

        while (toDelete.Count > 0)
        {
            toDelete[0].Remove();
            toDelete.RemoveAt(0);
        }
    }

I'm trying to cleans the Xml element with the attribut "nil=true" inside a document.
I come up with this algo but I don't like how it's look.

Do anyone know a linq version of this algo?

    /// <summary>
    /// Cleans the Xml element with the attribut "nil=true".
    /// </summary>
    /// <param name="value">The value.</param>
    public static void CleanNil(this XElement value)
    {
        List<XElement> toDelete = new List<XElement>();

        foreach (var element in value.DescendantsAndSelf())
        {
            if (element != null)
            {
                bool blnDeleteIt = false;
                foreach (var attribut in element.Attributes())
                {
                    if (attribut.Name.LocalName == "nil" && attribut.Value == "true")
                    {
                        blnDeleteIt = true;
                    }
                }
                if (blnDeleteIt)
                {
                    toDelete.Add(element);
                }
            }
        }

        while (toDelete.Count > 0)
        {
            toDelete[0].Remove();
            toDelete.RemoveAt(0);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巨坚强 2025-01-11 15:59:45

nil 属性的命名空间是什么?将其放入 { } 内,如下所示:

public static void CleanNil(this XElement value)
{
    value.Descendants().Where(x=> (bool?)x.Attribute("{http://www.w3.org/2001/XMLSchema-instance}nil") == true).Remove();
}

What's the namespace of the nil attribute? Put that inside { } like this:

public static void CleanNil(this XElement value)
{
    value.Descendants().Where(x=> (bool?)x.Attribute("{http://www.w3.org/2001/XMLSchema-instance}nil") == true).Remove();
}
友欢 2025-01-11 15:59:45

这应该有效..

public static void CleanNil(this XElement value)
{
     var todelete = value.DescendantsAndSelf().Where(x => (bool?) x.Attribute("nil") == true);
     if(todelete.Any())
     {
        todelete.Remove();
     }
}

This should work..

public static void CleanNil(this XElement value)
{
     var todelete = value.DescendantsAndSelf().Where(x => (bool?) x.Attribute("nil") == true);
     if(todelete.Any())
     {
        todelete.Remove();
     }
}
纸伞微斜 2025-01-11 15:59:45

扩展方法:

public static class Extensions
{
    public static void CleanNil(this XElement value)
    {
        value.DescendantsAndSelf().Where(x => x.Attribute("nil") != null && x.Attribute("nil").Value == "true").Remove();
    }
}

示例用法:

File.WriteAllText("test.xml", @"
                <Root nil=""false"">
                    <a nil=""true""></a>
                    <b>2</b>
                    <c nil=""false"">
                        <d nil=""true""></d>
                        <e nil=""false"">4</e>
                    </c>
                </Root>");
var root = XElement.Load("test.xml");
root.CleanNil();
Console.WriteLine(root);

输出:

<Root nil="false">
  <b>2</b>
  <c nil="false">
    <e nil="false">4</e>
  </c>
</Root>

如您所见,节点 按预期删除。唯一需要注意的是,您不能在 节点上调用此方法,因为无法删除根节点,并且您将收到以下运行时错误:

父母失踪。

The extension method:

public static class Extensions
{
    public static void CleanNil(this XElement value)
    {
        value.DescendantsAndSelf().Where(x => x.Attribute("nil") != null && x.Attribute("nil").Value == "true").Remove();
    }
}

Sample usage:

File.WriteAllText("test.xml", @"
                <Root nil=""false"">
                    <a nil=""true""></a>
                    <b>2</b>
                    <c nil=""false"">
                        <d nil=""true""></d>
                        <e nil=""false"">4</e>
                    </c>
                </Root>");
var root = XElement.Load("test.xml");
root.CleanNil();
Console.WriteLine(root);

output:

<Root nil="false">
  <b>2</b>
  <c nil="false">
    <e nil="false">4</e>
  </c>
</Root>

as you can see, nodes <a> and <d> where removed as expected. The only thing to note is that you cannot call this method on the <Root> node because the root node cannot be removed, and you will get this run-time error:

The parent is missing.

冷︶言冷语的世界 2025-01-11 15:59:45

我改变了解决该问题的方法,并避免在可为空类型上创建 nil
我使用以下

http ://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.90%29.aspx

    public class OptionalOrder
    {
        // This field should not be serialized 
        // if it is uninitialized.
        public string FirstOrder;

        // Use the XmlIgnoreAttribute to ignore the 
        // special field named "FirstOrderSpecified".
        [System.Xml.Serialization.XmlIgnoreAttribute]
        public bool FirstOrderSpecified;
    }

I change my way to solve that issue and avoid to create nil on my nullable type
I use the following

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer%28v=vs.90%29.aspx

    public class OptionalOrder
    {
        // This field should not be serialized 
        // if it is uninitialized.
        public string FirstOrder;

        // Use the XmlIgnoreAttribute to ignore the 
        // special field named "FirstOrderSpecified".
        [System.Xml.Serialization.XmlIgnoreAttribute]
        public bool FirstOrderSpecified;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文