1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
private void bt_split_1_file_Click(object sender, EventArgs e)
{
// Nom du type de noeud à splitter
string nodeToSplit = string.Empty;
// Récupération du nom sélectionné dans les radiobuttons (nom des noeuds à comparer)
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is RadioButton)
{
if ((ctrl as RadioButton).Checked == true)
{
nodeToSplit = ctrl.Text;
selectedRadioButton = nodeToSplit;
break;
}
}
}
string splitXpath = ".";
int i = 1;
//xmlNodeLevel est le niveau renseigné dans le Winform
while (i < xmlNodeLevel)
{
splitXpath += "/*";
i++;
}
splitXpath = splitXpath.Substring(0, splitXpath.Length - 1) + nodeToSplit;
string currentNodeName = string.Empty;
XmlDocument xmlSplit = new XmlDocument();
//Création du noeud Root
xmlSplit.LoadXml("<Root></Root>");
xmlSplit.CreateXmlDeclaration("1.0", "UTF8", null);
foreach (XmlNode currentNode in xmlFile1.DocumentElement.SelectNodes(splitXpath,mgr1))
{
if (currentNode.Name != currentNodeName)
{
currentNodeName = currentNode.Name;
XmlNode newNode = xmlSplit.ImportNode(currentNode, true);
xmlSplit.DocumentElement.AppendChild(newNode);
}
else
{
XmlNode newNode = xmlSplit.ImportNode(currentNode, true);
xmlSplit.DocumentElement.AppendChild(newNode);
}
}
//Sauvegarde du nouveau document généré avec un nom reprenant le nom d'origine du fichier avec "_split_....." derrière et l'enregistre au même endroit que le fichier d'origine.
xmlSplit.Save(textBox1.Text.Substring(0, textBox1.Text.Length - 4) + "_split_" + currentNodeName.Substring(currentNodeName.LastIndexOf(":") + 1) + ".xml");
MessageBox.Show("Split des fichiers terminé avec succès");
} |
Partager