基本上我是通过我作为参数接收的列表在VB.Net中创建新XML文档的结构。现在我可以创建具有不同属性和东西的XML而没有任何问题,但我想通过属性值(CodNivel1,CodNivel2,CodNivel3等)对元素进行分组。 我已经看到很多人使用linq对元素进行分组,但我不知道因为我对linq不熟悉。
我的功能
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
这是XML
<Parametros Fecha="12:00:00 a.m.">
<Niveles>
<Nivel1 CodNivel1="00-00-00-00" DesNivel1="AFG">
<Nivel2 CodNivel2="01-00-00-00" DesNivel2="LEVEL2">
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="LEVEL3">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="LEVEL4">
<Employeeinfo.../>
</Nivel4>
</Nivel3>
</Nivel2>
<Nivel2 CodNivel2="01-00-00-00" DesNivel2="LEVEL2">
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="LEVEL3">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="LEVEL4">
<Employeeinfo.../>
</Nivel4>
</Nivel3>
</Nivel2>
<Nivel2 CodNivel2="01-00-00-00" DesNivel2="LEVEL2">
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="LEVEL3">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="LEVEL4">
<Employeeinfo.../>
</Nivel4>
</Nivel3>
</Nivel2>
<Nivel2 CodNivel2="01-00-00-00" DesNivel2="LEVEL2">
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="LEVEL3">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="LEVEL4 B">
<Employeeinfo.../>
</Nivel4>
</Nivel3>
<Nivel3 CodNivel3="01-10-00-00" DesNivel3="LEVEL3">
<Nivel4 CodNivel4="01-10-10-00" DesNivel4="LEVEL4 B">
<Employeeinfo.../>
</Nivel4>
</Nivel3>
</Nivel2>
</Nivel1>
</Niveles>
</Parametros>
主要思想是创建一个像这样的XML
<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>
而不是像实际的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>
任何想法或建议都会很棒 感谢