Advertisement
Fhernd

R0601.cs

Nov 24th, 2015
2,889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Xml;
  5.  
  6. namespace R0601
  7. {
  8.     public partial class R0601 : Form
  9.     {
  10.         public R0601()
  11.         {
  12.             InitializeComponent();
  13.         }
  14.  
  15.         private void R0601_Load(object sender, EventArgs e)
  16.         {
  17.             txtRutaXml.Text = Path.Combine(Application.StartupPath, @"CatalogoProductos.xml");
  18.         }
  19.        
  20.         private void btnXml_Click(object sender, System.EventArgs e)
  21.         {
  22.             // Limpia el componente TreeView:
  23.             trvXml.Nodes.Clear();
  24.            
  25.             // Car el documento XML:
  26.             XmlDocument docXml = new XmlDocument();
  27.            
  28.             try
  29.             {
  30.                 docXml.Load(txtRutaXml.Text);
  31.             }
  32.             catch(Exception ex)
  33.             {
  34.                 MessageBox.Show(this, "Problema al cargar el documento XML.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  35.                 return;
  36.             }
  37.            
  38.             // Carga el contenido en el componente TreeView:
  39.             ConvertirXmlNodeATreeNode(docXml, trvXml.Nodes);
  40.            
  41.             // Expande todos los nodos:
  42.             trvXml.Nodes[0].ExpandAll();
  43.         }
  44.        
  45.         private void ConvertirXmlNodeATreeNode(XmlNode nodoXml, TreeNodeCollection nodos)
  46.         {
  47.             // Agregar un objeto TreeNode que represnte el nodo XmlNode actual:
  48.             TreeNode nuevoTreeNode = nodos.Add(nodoXml.Name);
  49.            
  50.             // Determina el elemento a mostrar:
  51.             switch(nodoXml.NodeType)
  52.             {
  53.                 case XmlNodeType.ProcessingInstruction:
  54.                 case XmlNodeType.XmlDeclaration:
  55.                     nuevoTreeNode.Text = String.Format("<?{0} {1}?>", nodoXml.Name, nodoXml.Value) ;
  56.                     break;
  57.                 case XmlNodeType.Element:
  58.                     nuevoTreeNode.Text = String.Format("<{0}>", nodoXml.Name);
  59.                     break;
  60.                 case XmlNodeType.Attribute:
  61.                     nuevoTreeNode.Text = String.Format("ATTRIBUTE: {0}", nodoXml.Name);
  62.                     break;
  63.                 case XmlNodeType.Text:
  64.                 case XmlNodeType.CDATA:
  65.                     nuevoTreeNode.Text = String.Format("{0}", nodoXml.Value);
  66.                     break;
  67.                 case XmlNodeType.Comment:
  68.                     nuevoTreeNode.Text = String.Format("<!--{0}-->", nodoXml.Value);
  69.                     break;
  70.             }
  71.            
  72.             // Invocación recursiva por cada atributo del elemento:
  73.             if (nodoXml.Attributes != null)
  74.             {
  75.                 foreach(XmlAttribute atributo in nodoXml.Attributes)
  76.                 {
  77.                     ConvertirXmlNodeATreeNode(atributo, nuevoTreeNode.Nodes);
  78.                 }
  79.             }
  80.            
  81.             // Invocación recursiva para cada nodo hijo:
  82.             foreach(XmlNode nodoHijo in nodoXml.ChildNodes)
  83.             {
  84.                 ConvertirXmlNodeATreeNode(nodoHijo, nuevoTreeNode.Nodes);
  85.             }
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement