Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Blogs / Rashed Amini / Remove Empty XML Nodes from...

Remove Empty XML Nodes from XMLPort

Rashed Profile Picture Rashed 3,765

XMLPort is a nice tool that allows you quickly import and export xml files.   The tool has some shortcomings, but there is always a work around it.

For an implementation recently  I had to export an xml and it could not have empty xml nodes.  For example.  Bellow is an xml file created by xmlport

 

<root>
  <Books>
    <Book>
      <Title>When the moon is low</Title>
      <Author>Nadia Hashimi</Author>
      <Category>Fiction</Category>
      <Summary></Summary>
      <Publisher>HarperCollins</Publisher>
      <Year>2018</Year>
      <ISBN>9780062421951</ISBN>
    </Book>
   </Books>
</root>

 

as you can see above the <Summary></Summary>  is empty and there is no way in XMLPORT to remove this node.

 

The workaround is to add post processing process that removes the nodes from xml file. 

 

xmlDoc := xmlDoc.XmlDocument;
xmlDoc.Load(FileName);



XMLNodeList := xmlDoc.SelectNodes('//Summary[. = ""]');
FOR i := 0 TO XMLNodeList.Count - 1 DO BEGIN
   XMLNode := XMLNodeList.Item(i);
   XMLNode.ParentNode().RemoveChild(XMLNode);
END;

xmlDoc.Save(FileName);

 

Xmlport has a property for each line called MinOccurs  But it only works for importing file.  I wish they would have a new property or add to this property to not write the node if it's empty. 

Comments

*This post is locked for comments