Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created December 3, 2015 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/1f6afc94e3e612f19af5 to your computer and use it in GitHub Desktop.
Save Fhernd/1f6afc94e3e612f19af5 to your computer and use it in GitHub Desktop.
Clase ayudante para la creación de elementos y atributos en un documento XML.
using System;
using System.Xml;
namespace Recetas.R0603
{
public class AyudanteXml
{
// Agregar un nuevo nodo un documento XML:
public static XmlNode AgregarElemento(XmlNode nodoPadre,
string nombreEtiqueta, string contenido)
{
// Crear el nuevo nodo:
XmlNode nuevoNodo = nodoPadre.OwnerDocument.CreateElement(nombreEtiqueta);
// Agrega el nuevo nodo en el nodo padre:
nodoPadre.AppendChild(nuevoNodo);
// Valida si el nodo contendrá un nodo de texto:
if (contenido != null)
{
XmlNode nodoContenido ;
nodoContenido = nodoPadre.OwnerDocument.CreateTextNode(contenido);
nuevoNodo.AppendChild(nodoContenido);
}
return nuevoNodo;
}
// Agregar un nuevo atributo a un documento XML:
public static XmlNode AgregarAtributo(XmlNode nodoPadre,
string nombreAtributo, string contenido)
{
XmlAttribute nuevoAtributo;
nuevoAtributo = nodoPadre.OwnerDocument.CreateAttribute(nombreAtributo);
nuevoAtributo.Value = contenido;
nodoPadre.Attributes.Append(nuevoAtributo);
return nuevoAtributo;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment