// find child nodes for current node private List <XmlPlusItem> FindChildList(XmlPlusItem parent, List <XmlPlusItem> PathList, int xIdx) { int childIdx; List <XmlPlusItem> childList = new List <XmlPlusItem>(); //Boolean printNode = true; for (childIdx = xIdx + 1; childIdx < PathList.Count; childIdx++) { XmlPlusItem xpi = PathList[childIdx]; //if (chkTextOnly.Checked) //{ // if (xpi.node.Attributes.Count > 0 || (xpi.NodeText != null && xpi.NodeText != "")) // { // printNode = true; // } //} //else //{ // printNode = true; //} //if (printNode) { Boolean ok = true; if (Ignore != null) { foreach (string s in Ignore) { if (s.Length > 0) { if (xpi.Path.IndexOf(s) >= 0) { ok = false; break; } } } } if (ok) { ok = Finder(xpi.Path); } if (ok) { if (xpi.PathNs.StartsWith(parent.PathNs)) { childList.Add(xpi); } } } } return(childList); }
private static void doIterateNode( XmlNode node , ref List <XmlPlusItem> PathList, string NameSpace) { XmlPlusItem xpi = new XmlPlusItem(); xpi.node = node; Boolean ok; switch (node.NodeType) { case XmlNodeType.Attribute: ok = false; break; case XmlNodeType.Element: ok = true; break; case XmlNodeType.Text: ok = false; break; case XmlNodeType.Document: ok = false; break; case XmlNodeType.XmlDeclaration: ok = false; break; default: ok = false; break; } if (ok) { xpi.Path = FindXPath(node); xpi.PathNs = UseDefaultNameSpace(xpi.Path, NameSpace); if (node.ChildNodes.Count == 1) { if (node.FirstChild.NodeType == XmlNodeType.Text) { xpi.NodeText = node.FirstChild.InnerText; } } PathList.Add(xpi); foreach (XmlNode childNode in node.ChildNodes) { doIterateNode(childNode, ref PathList, NameSpace); } } }
private void btnStart_Click(object sender, EventArgs e) { txtState.Text = "scan started"; if (txtIgnore.Text != "") { Ignore = txtIgnore.Text.Split(';'); } else { Ignore = null; } if (txtFind.Text != "") { Find = txtFind.Text.Split('/'); } else { Find = null; } if (txtTail.Text != "") { Tails = txtTail.Text.Split(';'); } else { Tails = null; } if (txtXML.Text != "") { XmlDocument xdoc = new XmlDocument(); try { xdoc.Load(txtXML.Text); List <XmlPlusItem> PathList = XmlTools.IterateThroughAllNodes(xdoc, txtNS.Text); StringBuilder sb = new StringBuilder(); Boolean printNode = true; int xIdx; for (xIdx = 0; xIdx < PathList.Count; xIdx++) { XmlPlusItem xpi = PathList[xIdx]; printNode = false; if (chkTextOnly.Checked) { if (xpi.node.Attributes.Count > 0 || (xpi.NodeText != null && xpi.NodeText != "")) { printNode = true; } } else { printNode = true; } if (printNode) { Boolean ok = true; if (Ignore != null) { foreach (string s in Ignore) { if (s.Length > 0) { if (xpi.Path.IndexOf(s) >= 0) { ok = false; break; } } } } if (ok) { ok = Finder(xpi.Path); } if (ok) { sb.AppendLine("<!-- ------------------------------------------------------------------ -->"); sb.AppendLine("<!-- XPath : --> " + xpi.Path); sb.AppendLine(""); sb.AppendLine("<!-- Node : --> " + XmlTools.NodeSelf(xpi.node)); sb.AppendLine(""); if (chkXPATH.Checked) { sb.AppendLine("<!-- XPath+NS : --> " + xpi.PathNs); sb.AppendLine(""); } if (chkTemplate.Checked) { sb.Append("<!-- Template : --> <xsl:template match=\"" + xpi.PathNs + "\">"); if (xpi.node.Attributes.Count > 0) { sb.Append("<xsl:value-of select=\"@" + xpi.node.Attributes[0].Name + "\"/>"); } if (xpi.NodeText != null && xpi.NodeText != "") { sb.Append("<xsl:value-of select=\".\"/>"); } sb.AppendLine("</xsl:template>"); sb.AppendLine(""); } // if (xpi.NodeText != null && xpi.NodeText != "") { List <XmlPlusItem> ChildList = null; if (chkIf.Checked || chkIfPost.Checked) { ChildList = FindChildList(xpi, PathList, xIdx); if (ChildList.Count > 0) { System.Diagnostics.Debug.Print(xpi.PathNs); } } if (chkDirect.Checked) { sb.Append("<!-- Direct : --> "); sb.AppendLine("<xsl:value-of select=\"" + xpi.PathNs + "\"/>"); sb.AppendLine(""); } if (chkIfPost.Checked) { sb.Append(" < !-- If Post : --> "); if (ChildList != null && ChildList.Count > 0) { sb.AppendLine(@"<xsl:if test =""" + xpi.PathNs + @" != '' "); foreach (XmlPlusItem child in ChildList) { sb.AppendLine(@" or " + child.PathNs + @" != '' "); } sb.AppendLine(@""" > "); sb.AppendLine(@"<xsl:call-template name=""PostProcess""> <xsl:with-param name=""size"" select=""0"" /> <xsl:with-param name=""string"" select=""" + xpi.PathNs + @"""/></xsl:call-template>"); sb.AppendLine(@"</xsl:if>"); } else { sb.AppendLine(@"<xsl:if test =""" + xpi.PathNs + @" != ''"">"); sb.AppendLine(@"<xsl:call-template name=""PostProcess""> <xsl:with-param name=""size"" select=""0"" /> <xsl:with-param name=""string"" select=""" + xpi.PathNs + @"""/></xsl:call-template>"); sb.AppendLine(@"</xsl:if>"); } sb.AppendLine(""); } if (chkPostProcess.Checked) { sb.Append(" < !-- Post Prc.: --> "); sb.AppendLine(@"<xsl:call-template name=""PostProcess""><xsl:with-param name=""string"" select=""" + xpi.PathNs + @""" /><xsl:with-param name=""size"" select=""0"" /></xsl:call-template>"); sb.AppendLine(""); } if (chkIf.Checked) { sb.Append("<!-- If-value : --> "); if (ChildList != null && ChildList.Count > 0) { sb.AppendLine(@"<xsl:if test =""" + xpi.PathNs + @" != '' "); foreach (XmlPlusItem child in ChildList) { sb.AppendLine(@" or " + child.PathNs + @" != '' "); } sb.AppendLine(@""" "); sb.AppendLine(@" > " + txtAddText.Text + @"<xsl:value-of select=""" + xpi.PathNs + @"""/></xsl:if >"); } else { sb.AppendLine(@"<xsl:if test =""" + xpi.PathNs + @" != ''"" >" + txtAddText.Text + @"<xsl:value-of select=""" + xpi.PathNs + @"""/></xsl:if >"); } sb.AppendLine(""); } if (chkFor.Checked) { sb.Append("<!-- For : --> "); sb.AppendLine(@"<xsl:for-each select=""" + xpi.PathNs + @"""> " + txtAddText.Text + @"<xsl:value-of select ="".""/></xsl:for-each>"); sb.AppendLine(""); } } if (xpi.NodeText != null && xpi.NodeText != "") { sb.AppendLine("<!-- Node text: --> <!-- " + xpi.NodeText + " -->"); sb.AppendLine(""); } sb.AppendLine(); } } } txtOut.Text = sb.ToString(); txtState.Text = "scan finished"; }catch (System.Exception ex) { MessageBox.Show(ex.Message); } } else { txtState.Text = "xml file not selected"; } }