Skip to content

Commit 19a607e

Browse files
committed
Save translation with self-closing XML element tags
1 parent 6a03d34 commit 19a607e

2 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/XmlContentTranslator/Main.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private bool OpenFirstXmlDocument(XmlDocument doc)
9999
var treeNode = new TreeNode(childNode.Name);
100100
treeNode.Tag = childNode;
101101
treeView1.Nodes.Add(treeNode);
102-
if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode))
102+
if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode())
103103
{
104104
ExpandNode(treeNode, childNode);
105105
}
@@ -199,7 +199,7 @@ private void OpenSecondFile(string fileName)
199199
{
200200
foreach (XmlNode childNode in doc.DocumentElement.ChildNodes)
201201
{
202-
if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode))
202+
if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode())
203203
{
204204
ExpandNode(null, childNode);
205205
}
@@ -307,7 +307,7 @@ private void ExpandNode(TreeNode parentNode, XmlNode node)
307307
treeView1.Nodes.Add(treeNode);
308308
else
309309
parentNode.Nodes.Add(treeNode);
310-
if (XmlUtils.IsParentElement(childNode))
310+
if (childNode.IsParentElement())
311311
{
312312
ExpandNode(treeNode, childNode);
313313
}
@@ -324,7 +324,7 @@ private void ExpandNode(TreeNode parentNode, XmlNode node)
324324
AddAttributes(node);
325325
foreach (XmlNode childNode in node.ChildNodes)
326326
{
327-
if (XmlUtils.IsParentElement(childNode))
327+
if (childNode.IsParentElement())
328328
{
329329
ExpandNode(null, childNode);
330330
}
@@ -423,7 +423,7 @@ private void FillOriginalDocumentFromSecondLanguage()
423423
{
424424
foreach (XmlNode childNode in _originalDocument.DocumentElement.ChildNodes)
425425
{
426-
if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode))
426+
if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode())
427427
{
428428
FillOriginalDocumentExpandNode(childNode);
429429
}
@@ -437,6 +437,7 @@ private void FillOriginalDocumentFromSecondLanguage()
437437
FillAttributes(_originalDocument.DocumentElement);
438438
}
439439
}
440+
XmlUtils.ConvertToSelfClosingTags(_originalDocument.DocumentElement);
440441
}
441442
}
442443

@@ -445,7 +446,7 @@ private void FillOriginalDocumentExpandNode(XmlNode node)
445446
FillAttributes(node);
446447
foreach (XmlNode childNode in node.ChildNodes)
447448
{
448-
if (childNode.ChildNodes.Count > 0 && !XmlUtils.IsTextNode(childNode))
449+
if (childNode.ChildNodes.Count > 0 && !childNode.IsTextNode())
449450
{
450451
FillOriginalDocumentExpandNode(childNode);
451452
}
@@ -463,7 +464,7 @@ private void FillOriginalDocumentExpandNode(XmlNode node)
463464

464465
private void FillAttributes(XmlNode node)
465466
{
466-
if (node.Attributes == null)
467+
if (node == null || node.Attributes == null)
467468
return;
468469

469470
foreach (XmlNode attribute in node.Attributes)

src/XmlContentTranslator/XmlUtils.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ public static class XmlUtils
1010
{
1111
private static readonly StringBuilder Sb = new StringBuilder();
1212

13-
public static bool IsTextNode(XmlNode childNode)
13+
public static bool IsTextNode(this XmlNode node)
1414
{
15-
if (childNode.ChildNodes.Count == 1 && childNode.ChildNodes[0].NodeType == XmlNodeType.Text)
16-
return true;
17-
return false;
15+
return node != null && node.ChildNodes.Count == 1 && node.ChildNodes[0].NodeType == XmlNodeType.Text;
16+
}
17+
18+
public static bool IsParentElement(this XmlNode node)
19+
{
20+
return node != null && node.ChildNodes.Count > 0 && !node.IsTextNode() && node.NodeType != XmlNodeType.Comment && node.NodeType != XmlNodeType.CDATA;
1821
}
1922

2023
public static string BuildNodePath(XmlNode node)
@@ -94,10 +97,28 @@ public static string GetNodeIndex(XmlNode node)
9497
return string.Format("[{0}]", i);
9598
}
9699

97-
public static bool IsParentElement(XmlNode xnode)
100+
public static void ConvertToSelfClosingTags(XmlElement element)
98101
{
99-
return xnode.ChildNodes.Count > 0 && !IsTextNode(xnode) &&
100-
xnode.NodeType != XmlNodeType.Comment && xnode.NodeType != XmlNodeType.CDATA;
102+
if (element != null && !element.IsEmpty)
103+
{
104+
bool noChildElements = true;
105+
if (element.HasChildNodes)
106+
{
107+
for (XmlNode node = element.FirstChild; node != null; node = node.NextSibling)
108+
{
109+
if (node.NodeType == XmlNodeType.Element)
110+
{
111+
ConvertToSelfClosingTags(node as XmlElement);
112+
noChildElements = false;
113+
}
114+
}
115+
}
116+
if (noChildElements && element.InnerText.Length == 0 && element.ParentNode?.ParentNode?.ParentNode != null)
117+
{
118+
element.IsEmpty = true;
119+
}
120+
}
101121
}
122+
102123
}
103124
}

0 commit comments

Comments
 (0)