Transformation d'une classe en VB.net vers C# pour gerer mon XML
cs_megamario
Messages postés145Date d'inscriptionlundi 15 juin 2009StatutMembreDernière intervention14 février 2013
-
15 janv. 2010 à 15:20
cs_megamario
Messages postés145Date d'inscriptionlundi 15 juin 2009StatutMembreDernière intervention14 février 2013
-
17 janv. 2010 à 15:36
Bonjour à tous
Pour mon stage j'ai développé un programme en VB.net (2008). Pour mon projet au lycée je dois réécrire le code en C# avec 3 élèves.
Voici mon code en Vb.net
Imports System.Xml
Imports System.Xml.XmlNode
Imports System.Xml.XPath
Imports System.Xml.Linq
Imports System
Public Class GestionXML
Inherits Collection
Dim Doc As XmlDocument
Dim ListXml As XmlNodeList
Public Sub Load(ByVal filename As String, ByVal VerSup As String)
Try
Dim Typ As XmlNode
Dim Types As XmlNode
Dim Items As XmlNode
Dim Co As Collection
Doc = New Xml.XmlDocument
Doc.Load(filename)
ListXml = Doc.GetElementsByTagName("type")
CollMesure = New Hashtable
CollEntree = New Hashtable
For Each Typ In ListXml
If Typ.Attributes("version").Value = VerSup Then
For Each Types In Typ
If Types.LocalName = "mesures" Then
For Each Items In Types
Co = New Collection
Co.Nom = Items.SelectSingleNode("Nom").InnerText
Co.Obj = Items.SelectSingleNode("Obj").InnerText
Co.Ind = Items.SelectSingleNode("Ind").InnerText
Co.Format = Items.SelectSingleNode("Format").InnerText
Co.Unit = Items.SelectSingleNode("Unit").InnerText
Co.Selection = Items.SelectSingleNode("Selection").InnerText
Co.SelectInter = Items.SelectSingleNode("SelectInter").InnerText
Co.SeuilInter = Items.SelectSingleNode("SeuilInter").InnerText
Co.SelectCourbe = Items.SelectSingleNode("SelectCourbe").InnerText
CollMesure.Add(Co.Nom, Co)
Next
End If
If Types.LocalName = "entrees" Then
For Each Items In Types
Co = New Collection
Co.Nom = Items.SelectSingleNode("Nom").InnerText
Co.Obj = Items.SelectSingleNode("Obj").InnerText
Co.Ind = Items.SelectSingleNode("Ind").InnerText
Co.Format = Items.SelectSingleNode("Format").InnerText
Co.Unit = Items.SelectSingleNode("Unit").InnerText
Co.Selection = Items.SelectSingleNode("Selection").InnerText
Co.SelectInter = Items.SelectSingleNode("SelectInter").InnerText
Co.SelectCourbe = Items.SelectSingleNode("SelectCourbe").InnerText
CollEntree.Add(Co.Nom, Co)
Next
End If
Next
End If
Next
Catch ex As Exception
End Try
End Sub
'
'Ajout d'un contrôle
Public Sub WriteXML(ByVal filename As String, ByVal VerSup As String, ByVal Tp As String, ByVal k As String)
Try
Dim Elem, ElNom, ElObj, ElInd, ElFormat, ElUnit, ElSelection, ElSelectInter, ElSeuilInter, ElSelectCourbe As XmlElement
Dim Typ, Types As XmlNode
Doc = New Xml.XmlDocument
Doc.Load(filename)
Dim root As XmlNode = Doc.DocumentElement
ListXml = Doc.GetElementsByTagName("type")
Dim Co As Collection = CollMesure(k) ' Initialisation de la collection
If Tp "entrees" Then Co CollEntree(k)
For Each Typ In ListXml
If Typ.Attributes("version").Value = VerSup Then
For Each Types In Typ
If Types.LocalName = Tp Then
Elem = Doc.CreateElement("item")
ElNom = Doc.CreateElement("Nom")
ElObj = Doc.CreateElement("Obj")
ElInd = Doc.CreateElement("Ind")
ElFormat = Doc.CreateElement("Format")
ElUnit = Doc.CreateElement("Unit")
ElSelection = Doc.CreateElement("Selection")
ElSelectInter = Doc.CreateElement("SelectInter")
ElSeuilInter = Doc.CreateElement("SeuilInter")
ElSelectCourbe = Doc.CreateElement("SelectCourbe")
ElNom.InnerText = Co.Nom
ElObj.InnerText = Co.Obj
ElInd.InnerText = Co.Ind
ElFormat.InnerText = Co.Format
ElUnit.InnerText = Co.Unit
ElSelection.InnerText = Co.Selection
ElSelectInter.InnerText = Co.SelectInter
ElSeuilInter.InnerText = Co.SeuilInter
ElSelectCourbe.InnerText = Co.SelectCourbe
Elem.AppendChild(ElNom)
Elem.AppendChild(ElNom)
Elem.AppendChild(ElObj)
Elem.AppendChild(ElInd)
Elem.AppendChild(ElFormat)
Elem.AppendChild(ElUnit)
Elem.AppendChild(ElSelection)
Elem.AppendChild(ElSelectInter)
Elem.AppendChild(ElSeuilInter)
Elem.AppendChild(ElSelectCourbe)
Types.InsertAfter(Elem, Types.LastChild)
End If
Next
End If
Next
Doc.Save(filename)
Catch ex As Exception
MsgBox("Erreur d'ecriture")
End Try
End Sub
'Suppresion d'un contrôle
Public Sub SupXML(ByVal filename As String, ByVal VerSup As String, ByVal Tp As String, ByVal k As String)
Try
'Dim Elem, ElNom, ElObj, ElInd, ElFormat, ElUnit, ElSelection, ElSelectInter, ElSeuilInter As XmlElement
Dim Typ, Types, MonItem, mesItems As XmlNode
Dim i As Integer = 0
Dim LeNom As String
Doc = New Xml.XmlDocument
Doc.Load(filename)
Dim root As XmlNode = Doc.DocumentElement
ListXml = Doc.GetElementsByTagName("type")
Dim Co As Collection = CollMesure(k) ' Initialisation de la collection
If Tp "entrees" Then Co CollEntree(k)
LeNom = Co.Nom
For Each Typ In ListXml
If Typ.Attributes("version").Value = VerSup Then
For Each Types In Typ
If Types.LocalName = Tp Then
For Each MonItem In Types
For Each mesItems In MonItem
If mesItems.InnerText = LeNom Then
Types.RemoveChild(MonItem)
End If
Next
Next
End If
Next
i = i + 1
End If
Next
Doc.Save(filename)
Catch ex As Exception
MsgBox("Erreur de modification")
End Try
End Sub
'
'Création d'une sauvegarde de configuration d'une affaire
Public Sub CreationXML(ByVal NomFich As String, ByVal versSuper As String)
Try
Dim ElType As XmlElement, ElMes As XmlElement, Elem As XmlElement, _
ElNom As XmlElement, ElObj As XmlElement, ElInd As XmlElement, ElFormat As XmlElement, ElUnit As XmlElement, ElSelection As XmlElement, _
ElSelectInter As XmlElement, ElSeuilInter As XmlElement, ElSelectCourbe As XmlElement
'Dim Typ As XmlNode ', 'Types As XmlNode
Dim Doc As New XmlDocument
Doc.LoadXml("")
ElType = Doc.CreateElement("type")
ElType.SetAttribute("version", versSuper)
'Ajout des mesures
ElMes = Doc.CreateElement("mesures")
For i = 0 To CollMesure.Count - 1
Dim Co As Collection = CollMesure(CollMesure.Keys(i)) ' Initialisation de la collection
Elem = Doc.CreateElement("item")
ElNom = Doc.CreateElement("Nom")
ElObj = Doc.CreateElement("Obj")
ElInd = Doc.CreateElement("Ind")
ElFormat = Doc.CreateElement("Format")
ElUnit = Doc.CreateElement("Unit")
ElSelection = Doc.CreateElement("Selection")
ElSelectInter = Doc.CreateElement("SelectInter")
ElSeuilInter = Doc.CreateElement("SeuilInter")
ElSelectCourbe = Doc.CreateElement("SelectCourbe")
ElNom.InnerText = Co.Nom
ElObj.InnerText = Co.Obj
ElInd.InnerText = Co.Ind
ElFormat.InnerText = Co.Format
ElUnit.InnerText = Co.Unit
ElSelection.InnerText = Co.Selection
ElSelectInter.InnerText = Co.SelectInter
ElSeuilInter.InnerText = Co.SeuilInter
ElSelectCourbe.InnerText = Co.SelectCourbe
'Elem.AppendChild(ElNom)
Elem.AppendChild(ElNom)
Elem.AppendChild(ElObj)
Elem.AppendChild(ElInd)
Elem.AppendChild(ElFormat)
Elem.AppendChild(ElUnit)
Elem.AppendChild(ElSelection)
Elem.AppendChild(ElSelectInter)
Elem.AppendChild(ElSeuilInter)
Elem.AppendChild(ElSelectCourbe)
ElMes.InsertAfter(Elem, ElMes.LastChild)
Next
ElType.AppendChild(ElMes)
Doc.DocumentElement.AppendChild(ElType)
'Ajout des entrées
ElMes = Doc.CreateElement("entrees")
For i = 0 To CollEntree.Count - 1
Dim Co As Collection = CollEntree(CollEntree.Keys(i)) ' Initialisation de la collection
Elem = Doc.CreateElement("item")
ElNom = Doc.CreateElement("Nom")
ElObj = Doc.CreateElement("Obj")
ElInd = Doc.CreateElement("Ind")
ElFormat = Doc.CreateElement("Format")
ElUnit = Doc.CreateElement("Unit")
ElSelection = Doc.CreateElement("Selection")
ElSelectInter = Doc.CreateElement("SelectInter")
ElSeuilInter = Doc.CreateElement("SeuilInter")
ElSelectCourbe = Doc.CreateElement("SelectCourbe")
ElNom.InnerText = Co.Nom
ElObj.InnerText = Co.Obj
ElInd.InnerText = Co.Ind
ElFormat.InnerText = Co.Format
ElUnit.InnerText = Co.Unit
ElSelection.InnerText = Co.Selection
ElSelectInter.InnerText = Co.SelectInter
ElSeuilInter.InnerText = Co.SeuilInter
ElSeuilInter.InnerText = Co.SelectCourbe
'Elem.AppendChild(ElNom)
Elem.AppendChild(ElNom)
Elem.AppendChild(ElObj)
Elem.AppendChild(ElInd)
Elem.AppendChild(ElFormat)
Elem.AppendChild(ElUnit)
Elem.AppendChild(ElSelection)
Elem.AppendChild(ElSelectInter)
Elem.AppendChild(ElSeuilInter)
Elem.AppendChild(ElSelectCourbe)
ElMes.InsertAfter(Elem, ElMes.LastChild)
Next
ElType.AppendChild(ElMes)
Doc.DocumentElement.AppendChild(ElType)
'Sauvegarde
Doc.Save(NomFich)
Catch ex As Exception
MsgBox("Erreur d'ecriture : " & Err.Description & " (Code : " & Err.Number & ")")
End Try
End Sub
End Class
Voici le code converti par developerfusion.com
using System.Xml;
using System.Xml.XmlNode;
using System.Xml.XPath;
using System.Xml.Linq;
using System;
public class GestionXML : Collection
{
XmlDocument Doc;
XmlNodeList ListXml;
public void Load(string filename, string VerSup)
{
try {
XmlNode Typ = default(XmlNode);
XmlNode Types = default(XmlNode);
XmlNode Items = default(XmlNode);
Collection Co = default(Collection);
Doc = new Xml.XmlDocument();
Doc.Load(filename);
ListXml = Doc.GetElementsByTagName("type");
CollMesure = new Hashtable();
CollEntree = new Hashtable();
foreach (var Typ in ListXml) {
if (Typ.Attributes("version").Value == VerSup) {
foreach (var Types in Typ) {
if (Types.LocalName == "mesures") {
foreach (var Items in Types) {
Co = new Collection();
Co.Nom = Items.SelectSingleNode("Nom").InnerText;
Co.Obj = Items.SelectSingleNode("Obj").InnerText;
Co.Ind = Items.SelectSingleNode("Ind").InnerText;
Co.Format = Items.SelectSingleNode("Format").InnerText;
Co.Unit = Items.SelectSingleNode("Unit").InnerText;
Co.Selection = Items.SelectSingleNode("Selection").InnerText;
Co.SelectInter = Items.SelectSingleNode("SelectInter").InnerText;
Co.SeuilInter = Items.SelectSingleNode("SeuilInter").InnerText;
Co.SelectCourbe = Items.SelectSingleNode("SelectCourbe").InnerText;
CollMesure.Add(Co.Nom, Co);
}
}
if (Types.LocalName == "entrees") {
foreach (var Items in Types) {
Co = new Collection();
Co.Nom = Items.SelectSingleNode("Nom").InnerText;
Co.Obj = Items.SelectSingleNode("Obj").InnerText;
Co.Ind = Items.SelectSingleNode("Ind").InnerText;
Co.Format = Items.SelectSingleNode("Format").InnerText;
Co.Unit = Items.SelectSingleNode("Unit").InnerText;
Co.Selection = Items.SelectSingleNode("Selection").InnerText;
Co.SelectInter = Items.SelectSingleNode("SelectInter").InnerText;
Co.SelectCourbe = Items.SelectSingleNode("SelectCourbe").InnerText;
CollEntree.Add(Co.Nom, Co);
}
}
}
}
}
}
catch (Exception ex) {
}
}
//
//Ajout d'un contrôle
public void WriteXML(string filename, string VerSup, string Tp, string k)
{
try {
XmlElement Elem = default(XmlElement);
XmlElement ElNom = default(XmlElement);
XmlElement ElObj = default(XmlElement);
XmlElement ElInd = default(XmlElement);
XmlElement ElFormat = default(XmlElement);
XmlElement ElUnit = default(XmlElement);
XmlElement ElSelection = default(XmlElement);
XmlElement ElSelectInter = default(XmlElement);
XmlElement ElSeuilInter = default(XmlElement);
XmlElement ElSelectCourbe = default(XmlElement);
XmlNode Typ = default(XmlNode);
XmlNode Types = default(XmlNode);
Doc = new Xml.XmlDocument();
Doc.Load(filename);
XmlNode root = Doc.DocumentElement;
ListXml = Doc.GetElementsByTagName("type");
Collection Co = CollMesure(k);
// Initialisation de la collection
if (Tp "entrees") Co CollEntree(k);
foreach (var Typ in ListXml) {
if (Typ.Attributes("version").Value == VerSup) {
foreach (var Types in Typ) {
if (Types.LocalName == Tp) {
Elem = Doc.CreateElement("item");
ElNom = Doc.CreateElement("Nom");
ElObj = Doc.CreateElement("Obj");
ElInd = Doc.CreateElement("Ind");
ElFormat = Doc.CreateElement("Format");
ElUnit = Doc.CreateElement("Unit");
ElSelection = Doc.CreateElement("Selection");
ElSelectInter = Doc.CreateElement("SelectInter");
ElSeuilInter = Doc.CreateElement("SeuilInter");
ElSelectCourbe = Doc.CreateElement("SelectCourbe");
ElNom.InnerText = Co.Nom;
ElObj.InnerText = Co.Obj;
ElInd.InnerText = Co.Ind;
ElFormat.InnerText = Co.Format;
ElUnit.InnerText = Co.Unit;
ElSelection.InnerText = Co.Selection;
ElSelectInter.InnerText = Co.SelectInter;
ElSeuilInter.InnerText = Co.SeuilInter;
ElSelectCourbe.InnerText = Co.SelectCourbe;
Elem.AppendChild(ElNom);
Elem.AppendChild(ElNom);
Elem.AppendChild(ElObj);
Elem.AppendChild(ElInd);
Elem.AppendChild(ElFormat);
Elem.AppendChild(ElUnit);
Elem.AppendChild(ElSelection);
Elem.AppendChild(ElSelectInter);
Elem.AppendChild(ElSeuilInter);
Elem.AppendChild(ElSelectCourbe);
Types.InsertAfter(Elem, Types.LastChild);
}
}
}
}
Doc.Save(filename);
}
catch (Exception ex) {
Interaction.MsgBox("Erreur d'ecriture");
}
}
//Suppresion d'un contrôle
public void SupXML(string filename, string VerSup, string Tp, string k)
{
try {
//Dim Elem, ElNom, ElObj, ElInd, ElFormat, ElUnit, ElSelection, ElSelectInter, ElSeuilInter As XmlElement
XmlNode Typ = default(XmlNode);
XmlNode Types = default(XmlNode);
XmlNode MonItem = default(XmlNode);
XmlNode mesItems = default(XmlNode);
int i = 0;
string LeNom = null;
Doc = new Xml.XmlDocument();
Doc.Load(filename);
XmlNode root = Doc.DocumentElement;
ListXml = Doc.GetElementsByTagName("type");
Collection Co = CollMesure(k);
// Initialisation de la collection
if (Tp "entrees") Co CollEntree(k);
LeNom = Co.Nom;
foreach (var Typ in ListXml) {
if (Typ.Attributes("version").Value == VerSup) {
foreach (var Types in Typ) {
if (Types.LocalName == Tp) {
foreach (var MonItem in Types) {
foreach (var mesItems in MonItem) {
if (mesItems.InnerText == LeNom) {
Types.RemoveChild(MonItem);
}
}
}
}
}
i = i + 1;
}
}
Doc.Save(filename);
}
catch (Exception ex) {
Interaction.MsgBox("Erreur de modification");
}
}
//
//Création d'une sauvegarde de configuration d'une affaire
public void CreationXML(string NomFich, string versSuper)
{
try {
XmlElement ElType = default(XmlElement);
XmlElement ElMes = default(XmlElement);
XmlElement Elem = default(XmlElement);
XmlElement ElNom = default(XmlElement);
XmlElement ElObj = default(XmlElement);
XmlElement ElInd = default(XmlElement);
XmlElement ElFormat = default(XmlElement);
XmlElement ElUnit = default(XmlElement);
XmlElement ElSelection = default(XmlElement);
XmlElement ElSelectInter = default(XmlElement);
XmlElement ElSeuilInter = default(XmlElement);
XmlElement ElSelectCourbe = default(XmlElement);
//Dim Typ As XmlNode ', 'Types As XmlNode
XmlDocument Doc = new XmlDocument();
Doc.LoadXml("");
ElType = Doc.CreateElement("type");
ElType.SetAttribute("version", versSuper);
//Ajout des mesures
ElMes = Doc.CreateElement("mesures");
for (i = 0; i <= CollMesure.Count - 1; i++) {
Collection Co = CollMesure(CollMesure.Keys(i));
// Initialisation de la collection
Elem = Doc.CreateElement("item");
ElNom = Doc.CreateElement("Nom");
ElObj = Doc.CreateElement("Obj");
ElInd = Doc.CreateElement("Ind");
ElFormat = Doc.CreateElement("Format");
ElUnit = Doc.CreateElement("Unit");
ElSelection = Doc.CreateElement("Selection");
ElSelectInter = Doc.CreateElement("SelectInter");
ElSeuilInter = Doc.CreateElement("SeuilInter");
ElSelectCourbe = Doc.CreateElement("SelectCourbe");
ElNom.InnerText = Co.Nom;
ElObj.InnerText = Co.Obj;
ElInd.InnerText = Co.Ind;
ElFormat.InnerText = Co.Format;
ElUnit.InnerText = Co.Unit;
ElSelection.InnerText = Co.Selection;
ElSelectInter.InnerText = Co.SelectInter;
ElSeuilInter.InnerText = Co.SeuilInter;
ElSelectCourbe.InnerText = Co.SelectCourbe;
//Elem.AppendChild(ElNom)
Elem.AppendChild(ElNom);
Elem.AppendChild(ElObj);
Elem.AppendChild(ElInd);
Elem.AppendChild(ElFormat);
Elem.AppendChild(ElUnit);
Elem.AppendChild(ElSelection);
Elem.AppendChild(ElSelectInter);
Elem.AppendChild(ElSeuilInter);
Elem.AppendChild(ElSelectCourbe);
ElMes.InsertAfter(Elem, ElMes.LastChild);
}
ElType.AppendChild(ElMes);
Doc.DocumentElement.AppendChild(ElType);
//Ajout des entrées
ElMes = Doc.CreateElement("entrees");
for (i = 0; i <= CollEntree.Count - 1; i++) {
Collection Co = CollEntree(CollEntree.Keys(i));
// Initialisation de la collection
Elem = Doc.CreateElement("item");
ElNom = Doc.CreateElement("Nom");
ElObj = Doc.CreateElement("Obj");
ElInd = Doc.CreateElement("Ind");
ElFormat = Doc.CreateElement("Format");
ElUnit = Doc.CreateElement("Unit");
ElSelection = Doc.CreateElement("Selection");
ElSelectInter = Doc.CreateElement("SelectInter");
ElSeuilInter = Doc.CreateElement("SeuilInter");
ElSelectCourbe = Doc.CreateElement("SelectCourbe");
ElNom.InnerText = Co.Nom;
ElObj.InnerText = Co.Obj;
ElInd.InnerText = Co.Ind;
ElFormat.InnerText = Co.Format;
ElUnit.InnerText = Co.Unit;
ElSelection.InnerText = Co.Selection;
ElSelectInter.InnerText = Co.SelectInter;
ElSeuilInter.InnerText = Co.SeuilInter;
ElSeuilInter.InnerText = Co.SelectCourbe;
//Elem.AppendChild(ElNom)
Elem.AppendChild(ElNom);
Elem.AppendChild(ElObj);
Elem.AppendChild(ElInd);
Elem.AppendChild(ElFormat);
Elem.AppendChild(ElUnit);
Elem.AppendChild(ElSelection);
Elem.AppendChild(ElSelectInter);
Elem.AppendChild(ElSeuilInter);
Elem.AppendChild(ElSelectCourbe);
ElMes.InsertAfter(Elem, ElMes.LastChild);
}
ElType.AppendChild(ElMes);
Doc.DocumentElement.AppendChild(ElType);
//Sauvegarde
Doc.Save(NomFich);
}
catch (Exception ex) {
Interaction.MsgBox("Erreur d'ecriture : " + Err.Description + " (Code : " + Err.Number + ")");
}
}
}
***********
Pour la collection j'utilisais une "Hashtable", non dispo en C#. j'ai donc utilisé un "Dictionary", déjà je ne sait pas si on a exactement le même fonctionnement mais bon ce n'est pas mon problème pour le XML.
J'ai plusieurs souci car il ne me reconnait pas "foreach (var Typ in ListXml)" il me dit que Type ne peut pas être déclarer dans cette porté car elle modifierait la signification de Typ etc...
Même type pour "if (Typ.Attributes("version").Value == VerSup)" -> Conversion impossible de 'Object'...
Entre autre. Mais le reste du même type d'erreur.
Merci de votre aide
Technicien en électricité Industriel (39 ans), en cours de reconversion BTS IRIS 2eme Année.
Au lycée pendant l'année scolaire, et en stage pendant les vacances scolaire.
A voir également:
Transformation d'une classe en VB.net vers C# pour gerer mon XML
cs_megamario
Messages postés145Date d'inscriptionlundi 15 juin 2009StatutMembreDernière intervention14 février 2013 17 janv. 2010 à 15:36
Merci, pour l'info pour la Hashtable. Je vais continuer à chercher pour le XML.
Technicien en électricité Industriel (39 ans), en cours de reconversion BTS IRIS 2eme Année.
Au lycée pendant l'année scolaire, et en stage pendant les vacances scolaire.