Question

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);
        }
    }
Was it helpful?

Solution

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();
}

OTHER TIPS

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();
     }
}

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.

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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top