Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save translation with self-closing XML element tags #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/XmlContentTranslator/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -439,6 +439,7 @@ private void FillOriginalDocumentFromSecondLanguage()
FillAttributes(_originalDocument.DocumentElement);
}
}
XmlUtils.ConvertToSelfClosingTags(_originalDocument.DocumentElement);
}
}

Expand All @@ -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);
}
Expand All @@ -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)
Expand Down
35 changes: 28 additions & 7 deletions src/XmlContentTranslator/XmlUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
}

}
}