using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.Xml.XPath; using System.IO; namespace WindowsFormsApp1 { public partial class StartShell : Form { XmlDocument xpathTo = null; public const string SearchFilter = "xml files (*.xml)|*.xml|config files (*.config, xml)|*.config|All files (*.*)|*.*"; private string[] Levels = new string[100]; public StartShell() { InitializeComponent(); } private void BtnFromXml_Click(object sender, EventArgs e) { openFileDialog1.Title = "Fetch XML-file keeping new values"; openFileDialog1.Filter = SearchFilter; if (openFileDialog1.ShowDialog() == DialogResult.OK) { lblFromXml.Text = openFileDialog1.FileName; } } private void BtnToXml_Click(object sender, EventArgs e) { openFileDialog1.Title = "Fetch XML-file to be updated"; openFileDialog1.Filter = SearchFilter; if (openFileDialog1.ShowDialog() == DialogResult.OK) { lblToXml.Text = openFileDialog1.FileName; } } private void BtnStart_Click(object sender, EventArgs e) { // XML-document analysed XmlDocument xnode = new XmlDocument(); xnode.Load(lblFromXml.Text); if (lblToXml.Text != "toxmlpath") { // XML-document for receiving file xpathTo = new XmlDocument(); xpathTo.Load(lblToXml.Text); } lvwResult.Items.Clear(); Process(xnode); // Saves to temporary file xpathTo.Save("abc.xml"); // Move temporary file to original directory FileInfo fi = new FileInfo(lblToXml.Text); var actDir = fi.DirectoryName; var createdFile = File.ReadAllBytes("abc.xml"); var fName = actDir + "\\default_" + DateTime.Now.ToShortDateString()+"__" + DateTime.Now.ToLongTimeString().Replace(":","-") + ".config"; File.WriteAllBytes(fName,createdFile); File.Delete("abc.xml"); } void Process(XmlNode node) { // Processing is a recurring process Process(node, 0); } void Process(XmlNode node, int level) { if (node.NodeType == XmlNodeType.Text) { ListViewItem lviResult = new ListViewItem(); lviResult.Text = node.ParentNode.LocalName; lviResult.SubItems.Add(node.Value); // Find XPath for current node var xPath = GetXPathToNode(node); // Routine for XPath creates bad end on text-nodes / chenges to something seems working if (xPath.Trim().Substring(xPath.Trim().Length - 9) == "/#text[1]") { xPath = xPath.Substring(0, xPath.Trim().Length - 9); xPath = xPath + "/text()"; } // Put data into ListView lviResult.SubItems.Add(xPath); if (lblToXml.Text != "toxmlpath") { XmlNode searchedField = xpathTo.SelectSingleNode(xPath); if (searchedField != null) { lviResult.SubItems.Add(searchedField.InnerText); searchedField.Value = node.Value; } else { searchedField = xpathTo.SelectSingleNode(xPath.Substring(0, xPath.Trim().Length-7)); if (searchedField.OuterXml != "") { lviResult.SubItems.Add(searchedField.OuterXml); XmlElement elemSearchfield = (XmlElement) searchedField; if (elemSearchfield.IsEmpty) { elemSearchfield.InnerXml = node.Value; } } else lviResult.SubItems.Add("saknas!"); } } lvwResult.Items.Add(lviResult); } foreach (XmlNode child in node.ChildNodes) { Process(child, level + 1); } } /// /// Gets the X-Path to a given Node /// /// The Node to get the X-Path from /// The X-Path of the Node public string GetXPathToNode(XmlNode node) { if (node.NodeType == XmlNodeType.Attribute) { // attributes have an OwnerElement, not a ParentNode; also they have // to be matched by name, not found by position return String.Format("{0}/@{1}", GetXPathToNode(((XmlAttribute)node).OwnerElement), node.Name); } if (node.ParentNode == null) { // the only node with no parent is the root node, which has no path return ""; } // Get the Index int indexInParent = 1; XmlNode siblingNode = node.PreviousSibling; // Loop thru all Siblings while (siblingNode != null) { // Increase the Index if the Sibling has the same Name if (siblingNode.Name == node.Name) { indexInParent++; } siblingNode = siblingNode.PreviousSibling; } // the path to a node is the path to its parent, plus "/node()[n]", where n is its position among its siblings. return String.Format("{0}/{1}[{2}]", GetXPathToNode(node.ParentNode), node.Name, indexInParent); } private void LvwResult_Click(object sender, EventArgs e) { var firstSelectedItem = lvwResult.SelectedItems[0]; var xpath = firstSelectedItem.SubItems[2].Text; if (xpath.Trim().Substring(xpath.Trim().Length - 9) == "/#text[1]") { txtXPathAnalyse.Text = xpath.Substring(0, xpath.Trim().Length - 9); } else { txtXPathAnalyse.Text = xpath; } } } }