Group elements in XML by attribute



Basically I'm creating the structure of a new XML document in VB.Net through a list that I receive as a parameter. Right now I can create the XML with different attributes and stuff without any problem but I would like to group the elements by an attribute value (CodNivel1, CodNivel2, CodNivel3,etc ...) . I've seen that many people use linq to group the elements but I have no idea due to I'm kind of new to linq.


My function



Public Function CrearXmlEstructura(ByRef listaEstructuraOrg As List(Of Model.Estructura))

Dim fecha As New System.DateTime
Dim listaEstructura As New List(Of Model.Estructura)

listaEstructura = listaEstructuraOrg
Dim XmlResultado As New XmlDocument

Try
If (listaEstructura.Count > 0) Then

'agregamos Elemento Parametros
Dim XMLRaiz = XmlResultado.CreateElement("Parametros")
'Atributos = Fecha
Dim Atributo As XmlAttribute = XmlResultado.CreateAttribute("Fecha")
Atributo.Value = fecha
XMLRaiz.Attributes.Append(Atributo)
'Agregamos a la Raiz
XmlResultado.AppendChild(XMLRaiz)

'agregamos Elemento Niveles
Dim ElementoNiveles As XmlElement = XmlResultado.CreateElement("Niveles")
XMLRaiz.AppendChild(ElementoNiveles)

'agregamos Elemento Nivel1
Dim ElementoNivel1 As XmlElement = XmlResultado.CreateElement("Nivel1")
'Atributos = CodNivel1 y Descripción
Atributo = XmlResultado.CreateAttribute("CodNivel1")
Atributo.Value = listaEstructura(0).CodNivel1
ElementoNivel1.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("DesNivel1")
Atributo.Value = listaEstructura(0).DesNivel1
ElementoNivel1.Attributes.Append(Atributo)
ElementoNiveles.AppendChild(ElementoNivel1)

For Each registro In listaEstructura

'agregamos Elemento Nivel2
Dim ElementoNivel2 As XmlElement = XmlResultado.CreateElement("Nivel2")
'Atributos = CodNivel2 y Descripción
Atributo = XmlResultado.CreateAttribute("CodNivel2")
Atributo.Value = registro.CodNivel2
ElementoNivel2.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("DesNivel2")
Atributo.Value = registro.DesNivel2
ElementoNivel2.Attributes.Append(Atributo)
ElementoNivel1.AppendChild(ElementoNivel2)

'agregamos Elemento Nivel3
Dim ElementoNivel3 As XmlElement = XmlResultado.CreateElement("Nivel3")
'Atributos = CodNivel3 y Descripción
Atributo = XmlResultado.CreateAttribute("CodNivel3")
Atributo.Value = registro.CodNivel3
ElementoNivel3.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("DesNivel3")
Atributo.Value = registro.DesNivel3
ElementoNivel3.Attributes.Append(Atributo)
ElementoNivel2.AppendChild(ElementoNivel3)

If registro.CodNivel4.Trim().Equals(String.Empty) And registro.CodNivel5.Trim().Equals(String.Empty) Then
'agregamos Elemento InformacionEmpleados a Elemento Nivel3
Dim Nivel3InformacionEmpleados As XmlElement = XmlResultado.CreateElement("InformacionEmpleados")
'Atributos = Ubicacion, Cedula, Nombre, CodPuesto y Puesto
Atributo = XmlResultado.CreateAttribute("Ubicacion")
Atributo.Value = registro.Ubicacion
Nivel3InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Cedula")
Atributo.Value = registro.Cedula
Nivel3InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Nombre")
Atributo.Value = registro.Nombre
Nivel3InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("CodPuesto")
Atributo.Value = registro.CodPuesto
Nivel3InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Puesto")
Atributo.Value = registro.Puesto
Nivel3InformacionEmpleados.Attributes.Append(Atributo)
ElementoNivel3.AppendChild(Nivel3InformacionEmpleados) 'agregamos un elemento a Nivel3

Else

'agregamos Elemento Nivel4
Dim ElementoNivel4 As XmlElement = XmlResultado.CreateElement("Nivel4")
'Atributos = CodNivel4 y Descripción
Atributo = XmlResultado.CreateAttribute("CodNivel4")
Atributo.Value = registro.CodNivel4
ElementoNivel4.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("DesNivel4")
Atributo.Value = registro.DesNivel4
ElementoNivel4.Attributes.Append(Atributo)
ElementoNivel3.AppendChild(ElementoNivel4)

If registro.CodNivel5.Trim().Equals(String.Empty) Then
'agregamos Elemento InformacionEmpleados a Elemento Nivel4
Dim Nivel4InformacionEmpleados As XmlElement = XmlResultado.CreateElement("InformacionEmpleados")
'Atributos = Ubicacion, Cedula, Nombre, CodPuesto y Puesto
Atributo = XmlResultado.CreateAttribute("Ubicacion")
Atributo.Value = registro.Ubicacion
Nivel4InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Cedula")
Atributo.Value = registro.Cedula
Nivel4InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Nombre")
Atributo.Value = registro.Nombre
Nivel4InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("CodPuesto")
Atributo.Value = registro.CodPuesto
Nivel4InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Puesto")
Atributo.Value = registro.Puesto
Nivel4InformacionEmpleados.Attributes.Append(Atributo)
ElementoNivel4.AppendChild(Nivel4InformacionEmpleados) 'agregamos un elemento a Nivel4

Else
'agregamos Elemento Nivel5
Dim ElementoNivel5 As XmlElement = XmlResultado.CreateElement("Nivel5")
'Atributos = CodNivel5 y Descripción
Atributo = XmlResultado.CreateAttribute("CodNivel5")
Atributo.Value = registro.CodNivel5
ElementoNivel5.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("DesNivel5")
Atributo.Value = registro.DesNivel5
ElementoNivel5.Attributes.Append(Atributo)
ElementoNivel4.AppendChild(ElementoNivel5)

'agregamos Elemento InformacionEmpleados a Elemento Nivel5
Dim Nivel5InformacionEmpleados As XmlElement = XmlResultado.CreateElement("InformacionEmpleados")
'Atributos = Ubicacion, Cedula, Nombre, CodPuesto y Puesto
Atributo = XmlResultado.CreateAttribute("Ubicacion")
Atributo.Value = registro.Ubicacion
Nivel5InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Cedula")
Atributo.Value = registro.Cedula
Nivel5InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Nombre")
Atributo.Value = registro.Nombre
Nivel5InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("CodPuesto")
Atributo.Value = registro.CodPuesto
Nivel5InformacionEmpleados.Attributes.Append(Atributo)
Atributo = XmlResultado.CreateAttribute("Puesto")
Atributo.Value = registro.Puesto
Nivel5InformacionEmpleados.Attributes.Append(Atributo)
ElementoNivel5.AppendChild(Nivel5InformacionEmpleados) 'agregamos un elemento a Nivel5

End If
End If

Next
End If

Catch ex As Exception
Dim x = ex.Message
End Try
Return XmlResultado 'resultado
End Function


This is the XML



<Parametros Fecha="12:00:00 a.m.">
<Niveles>
<Nivel1 CodNivel1="00-00-00-00" DesNivel1="BANCO CENTRAL DE COSTA RICA">
<Nivel2 CodNivel2="01-00-00-00" DesNivel2="NIVEL EJECUTIVO">
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="PRESIDENCIA">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="PRESIDENCIA">
<InformacionEmpleados Ubicacion="4" Cedula="05-0232-0897" Nombre=" AFACDCAIJ AFACDCAIJ, JUAN AFACDCAIJ" CodPuesto="17" Puesto="Profesional Gestión Bancaria 4" />
</Nivel4>
</Nivel3>
</Nivel2>
<Nivel2 CodNivel2="01-00-00-00" DesNivel2="NIVEL EJECUTIVO">
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="PRESIDENCIA">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="PRESIDENCIA">
<InformacionEmpleados Ubicacion="5" Cedula="01-0986-0853" Nombre=" ABAJIGAIF ABAJIGAIF, JUAN ABAJIGAIF" CodPuesto="24" Puesto="Asistente Servicios Institucionales 1" />
</Nivel4>
</Nivel3>
</Nivel2>
<Nivel2 CodNivel2="01-00-00-00" DesNivel2="NIVEL EJECUTIVO">
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="PRESIDENCIA">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="PRESIDENCIA">
<InformacionEmpleados Ubicacion="3" Cedula="01-0846-0235" Nombre=" ABAIEGACD ABAIEGACD, JUAN ABAIEGACD" CodPuesto="25" Puesto="Asistente Servicios Institucionales 2" />
</Nivel4>
</Nivel3>
</Nivel2>
<Nivel2 CodNivel2="01-00-00-00" DesNivel2="NIVEL EJECUTIVO">
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="PRESIDENCIA">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="PRESIDENCIA">
<InformacionEmpleados Ubicacion="2" Cedula="01-0738-0483" Nombre=" ABAHDIAEI ABAHDIAEI, JUAN ABAHDIAEI" CodPuesto="32" Puesto="Profesional Gestión Bancaria 5" />
</Nivel4>
</Nivel3>
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="PRESIDENCIA">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="PRESIDENCIA">
<InformacionEmpleados Ubicacion="2" Cedula="01-0738-0483" Nombre=" ABAHDIAEI ABAHDIAEI, JUAN ABAHDIAEI" CodPuesto="32" Puesto="Profesional Gestión Bancaria 5" />
</Nivel4>
</Nivel3>
</Nivel2>
</Nivel1>
</Niveles>
</Parametros>


The main idea is to create a XML like this



<Level1 cod 00-00-00-00>
<Level2 cod 01-00-00-00
<Level3 cod 03-00-05-00>
<Employee info>
<Employee info>
<Level3 cod 03-05-05-00>
<Employee info>
<Employee info>
</Level2>
</Level1>


and not like the actual XML



<Level1 cod 00-00-00-00>
<Level2 cod 01-00-00-00>
<Level2 cod 01-00-00-00>
<Level2 cod 01-00-00-00>
<Level2 cod 01-00-00-00>
<Level3 cod 03-00-05-00>
Employess
<Level2 cod 02.02.00.00>
<LevelL2 cod 03.02.00.00>
</Level1>


Any ideas or suggestions would be awesome Thanks


No comments:

Post a Comment