diff --git a/src/XmlContentTranslator/Main.cs b/src/XmlContentTranslator/Main.cs index ee27ad5..45f00c2 100644 --- a/src/XmlContentTranslator/Main.cs +++ b/src/XmlContentTranslator/Main.cs @@ -101,7 +101,7 @@ private bool OpenFirstXmlDocument(XmlDocument doc) var treeNode = new TreeNode(childNode.Name); treeNode.Tag = childNode; treeView1.Nodes.Add(treeNode); - if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode)) + if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode()) { ExpandNode(treeNode, childNode); } @@ -201,7 +201,7 @@ private void OpenSecondFile(string fileName) { foreach (XmlNode childNode in doc.DocumentElement.ChildNodes) { - if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode)) + if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode()) { ExpandNode(null, childNode); } @@ -309,7 +309,7 @@ private void ExpandNode(TreeNode parentNode, XmlNode node) treeView1.Nodes.Add(treeNode); else parentNode.Nodes.Add(treeNode); - if (XmlUtils.IsParentElement(childNode)) + if (childNode.IsParentElement()) { ExpandNode(treeNode, childNode); } @@ -326,7 +326,7 @@ private void ExpandNode(TreeNode parentNode, XmlNode node) AddAttributes(node); foreach (XmlNode childNode in node.ChildNodes) { - if (XmlUtils.IsParentElement(childNode)) + if (childNode.IsParentElement()) { ExpandNode(null, childNode); } @@ -425,7 +425,7 @@ private void FillOriginalDocumentFromSecondLanguage() { foreach (XmlNode childNode in _originalDocument.DocumentElement.ChildNodes) { - if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode)) + if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode()) { FillOriginalDocumentExpandNode(childNode); } @@ -439,6 +439,7 @@ private void FillOriginalDocumentFromSecondLanguage() FillAttributes(_originalDocument.DocumentElement); } } + XmlUtils.ConvertToSelfClosingTags(_originalDocument.DocumentElement); } } @@ -447,7 +448,7 @@ private void FillOriginalDocumentExpandNode(XmlNode node) FillAttributes(node); foreach (XmlNode childNode in node.ChildNodes) { - if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode)) + if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode()) { FillOriginalDocumentExpandNode(childNode); } @@ -465,7 +466,7 @@ private void FillOriginalDocumentExpandNode(XmlNode node) private void FillAttributes(XmlNode node) { - if (node.Attributes == null) + if (node == null || node.Attributes == null) return; foreach (XmlNode attribute in node.Attributes) diff --git a/src/XmlContentTranslator/XmlUtils.cs b/src/XmlContentTranslator/XmlUtils.cs index 2c39e0d..16fcf96 100644 --- a/src/XmlContentTranslator/XmlUtils.cs +++ b/src/XmlContentTranslator/XmlUtils.cs @@ -10,11 +10,14 @@ public static class XmlUtils { private static readonly StringBuilder Sb = new StringBuilder(); - public static bool IsTextNode(XmlNode childNode) + public static bool IsTextNode(this XmlNode node) { - if (childNode.ChildNodes.Count == 1 && childNode.ChildNodes[0].NodeType == XmlNodeType.Text) - return true; - return false; + return node != null && node.ChildNodes.Count == 1 && node.ChildNodes[0].NodeType == XmlNodeType.Text; + } + + public static bool IsParentElement(this XmlNode node) + { + return node != null && node.ChildNodes.Count > 0 && !node.IsTextNode() && node.NodeType != XmlNodeType.Comment && node.NodeType != XmlNodeType.CDATA; } public static string BuildNodePath(XmlNode node) @@ -94,10 +97,28 @@ public static string GetNodeIndex(XmlNode node) return string.Format("[{0}]", i); } - public static bool IsParentElement(XmlNode xnode) + public static void ConvertToSelfClosingTags(XmlElement element) { - return xnode.ChildNodes.Count > 0 && !IsTextNode(xnode) && - xnode.NodeType != XmlNodeType.Comment && xnode.NodeType != XmlNodeType.CDATA; + if (element != null && !element.IsEmpty) + { + bool noChildElements = true; + if (element.HasChildNodes) + { + for (XmlNode node = element.FirstChild; node != null; node = node.NextSibling) + { + if (node.NodeType == XmlNodeType.Element) + { + ConvertToSelfClosingTags(node as XmlElement); + noChildElements = false; + } + } + } + if (noChildElements && element.InnerText.Length == 0 && element.ParentNode?.ParentNode?.ParentNode != null) + { + element.IsEmpty = true; + } + } } + } }