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.