0

I have the following node in my xml file :

<PERSON>
  <LEOKA_DATA> </LEOKA_DATA>
</PERSON>

I am not able to remove the LEOKA tag using the following code (snippet):

        string file = CommonVariables.MainDir + @"\Schemas\RemoveEmptyTags.xsl";
        try
        {
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.Load(tempFile); //tempfile is the xml file

            XmlNodeList emptyElements = xmlDocument.SelectNodes(@"//*[not(node())]");

            if (emptyElements != null)

                for (int i = emptyElements.Count - 1; i >= 0; i--)
                {
                    var parentNode = emptyElements[i].ParentNode;

                    if (parentNode != null)
                    {
                        if (emptyElements[i].Name != "LINE")
                            parentNode.RemoveChild(emptyElements[i]);
                    }
                }
        }
        catch (FileNotFoundException ex)
        { **something here** }

However, the code above works if the node is like below (notice no space between start tag and end tag) :

<LEOKA></LEOKA> 

I also tried using the following code but didn't work :

var doc = XDocument.Parse(tempfile);

var emptyElements = from descendant in doc.Descendants()
                where string.IsNullOrWhiteSpace(descendant.Value)
                select descendant;

emptyElements.Remove();

Any help would be really appreciated. Please let me know if you need more details. Thanks.

rapidfire
  • 63
  • 6
  • possible duplicate of [Locating the node by value containing whitespaces using XPath](http://stackoverflow.com/questions/393840/locating-the-node-by-value-containing-whitespaces-using-xpath) – Justin Russo Feb 05 '15 at 14:56
  • You are looking for a tag which contains whitespace, and whitespace can consist of numerous characters, like, ' ' for tabs, or ' ' for a newline, etc... So, even a simple space character, equals a value. Thus, it won't find the node with the space character using not(node()). – Justin Russo Feb 05 '15 at 14:59
  • Even if you use, string-length(text()), it won't work because the string will have a length of 1with a space char. – Justin Russo Feb 05 '15 at 15:01
  • I suspect you haven't given us accurate criteria for what nodes you want to not select ("remove"). If you just want to exclude `` elements, then your XPath is just `//*[not(self::LEOKA_DATA)]`. So what are your actual criteria - exclude all elements who don't have any child nodes other than whitespace-only text? – LarsH Feb 05 '15 at 15:17
  • Thanks for the response. I wanted to remove all LEOKA elements which have no child nodes and white space values. I got it to work using the following expression --> [not(*) and not(text()[normalize-space()])] – rapidfire Feb 05 '15 at 20:54
  • 1
    @JustinRusso - Thanks for giving insight. Your link to the possible duplicate issue helped. – rapidfire Feb 05 '15 at 20:56

1 Answers1

0

I would modify your XPath expression as follows:

XmlNodeList emptyElements = xmlDocument.SelectNodes(@"//*[not(node()[
    not(self::text()[normalize-space() = ''])
  ])]");

In other words, exclude elements that have a child other than an empty text node, where "empty" means having no text value other than whitespace.

LarsH
  • 27,481
  • 8
  • 94
  • 152