Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Dear all,

I have below xml which I wanted to convert it into a generic list of class.
XML
<?xml version="1.0" encoding="utf-8"?>
<menuroot>
  <Menu id="home" text="Home">
    <subMenu id="Syllabusboard" text="Syllabusboard" url="Syllabusboard.aspx">
    
  
  <Menu id="Subjects" text="Subjects">
    <SubMenu id="SyllabusreView" text="SyllabusreView" url="SyllabusreViewReport.aspx">
    <SubMenu id="Subject " text="Subject Map" url="Subject.aspx">
    
  <Menu id="grade" text="Traceability">
    <SubMenu id="grade" text="grade Report" url=" gradeReport.aspx">
    
  
  <Menu id="testresults" text="Test results">
    <SubMenu id="testReport" text="Test Report" url="TestReport.aspx">
    
    <SubMenu id="testresultsforstudent" text="testresultsforstudent" url="testresultsforstudent.aspx">
    
  
  <Menu id="Students" text="Students">
    <SubMenu id="AttendaceLog" text="Attendace Log" url="Attendace.aspx">
    
    <SubMenu id="studentMpping" text="studentMpping" url="studentMpping.aspx">

In the above XML, I am able to attributes of Menu ID and Menu text. But not getting how to get values from <submenu> attributes.

Below is the code which I tried to implement this. But I want to know more efficient code than the below. Please help me the same.

VB
Dim attrAdminMenuObjects = XDocument.Parse(xmlAdminDoc.InnerXml).Descendants("Menu")
                    Dim objMenuSettings As New MenuSettings
 

                    For Each headerMenuNode In attrAdminMenuObjects.Elements
                        For Each childNode In headerMenuNode.Nodes
                            objMenuSettings .Menu_ID = headerMenuNode.FirstAttribute.Value
                           objMenuSettings .Menu_Text = headerMenuNode.LastAttribute.Value
                            Dim xeSubmenuElements As XElement = DirectCast(childNode, System.Xml.Linq.XElement)
                            If xeSubmenuElements.HasAttributes Then
                                Dim attrSubMenuElementList As IEnumerable(Of XAttribute) = From attrSubMenuelement In xeSubmenuElements.Attributes() Select attrSubMenuelement
                                For Each att In attrSubMenuElementList
                                    Select Case att.Name
                                        Case "id"
                                            objMenuSettings .SubMenu_ID = att.Value
                                        Case "text"
                                            objMenuSettings .SubMenu_Text = att.Value
                                        Case "url"
                                            objMenuSettings .URL = att.Value
                                        Case "Pagination"
                                            objMenuSettings .isPaginationRequired = att.Value
                                        Case "Filter"
                                            objMenuSettings .isFilterRequired = att.Value
                                        Case "Choose_Columns"
                                            objMenuSettings .ChooseColumns = att.Value
                                    End Select
                                    lstSiteMenuSettings.Add(objMenuSettings)
                                Next
                            End If

                        Next

                    Next


I wanted to know if there is any efficient way to implement this scenario. Please help me the same.

What I have tried:

VB
Dim attrAdminMenuObjects = XDocument.Parse(xmlAdminDoc.InnerXml).Descendants("Menu")
                    Dim objMenuSettings As New MenuSettings
 

                    For Each headerMenuNode In attrAdminMenuObjects.Elements
                        For Each childNode In headerMenuNode.Nodes
                            objMenuSettings .Menu_ID = headerMenuNode.FirstAttribute.Value
                           objMenuSettings .Menu_Text = headerMenuNode.LastAttribute.Value
                            Dim xeSubmenuElements As XElement = DirectCast(childNode, System.Xml.Linq.XElement)
                            If xeSubmenuElements.HasAttributes Then
                                Dim attrSubMenuElementList As IEnumerable(Of XAttribute) = From attrSubMenuelement In xeSubmenuElements.Attributes() Select attrSubMenuelement
                                For Each att In attrSubMenuElementList
                                    Select Case att.Name
                                        Case "id"
                                            objMenuSettings .SubMenu_ID = att.Value
                                        Case "text"
                                            objMenuSettings .SubMenu_Text = att.Value
                                        Case "url"
                                            objMenuSettings .URL = att.Value
                                        Case "Pagination"
                                            objMenuSettings .isPaginationRequired = att.Value
                                        Case "Filter"
                                            objMenuSettings .isFilterRequired = att.Value
                                        Case "Choose_Columns"
                                            objMenuSettings .ChooseColumns = att.Value
                                    End Select
                                    lstSiteMenuSettings.Add(objMenuSettings)
                                Next
                            End If

                        Next

                    Next
Posted
Updated 9-Apr-17 8:08am
v3
Comments
Maciej Los 7-Apr-17 13:44pm    
"I want to know more efficient code than the below" - this is not a question at all!
Sudhavamsikiran Damojipurapu 9-Apr-17 14:00pm    
My question is simple...I wanted to know if there is any efficient way to implement this scenario
[no name] 9-Apr-17 14:05pm    
Yes, profile your code and stop using LINQ.
Sudhavamsikiran Damojipurapu 9-Apr-17 14:08pm    
Thanks for the response. May I know a bit with more clarity on this.
[no name] 9-Apr-17 15:26pm    
Sure. Profile your code and stop using LINQ.

1 solution

As i mentioned in the comment to the question, your question is unclear...

At this moment i can say only:
The best way to "convert" xml data into list of classes is to use serialization[^]. See:
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[^]
Examples of XML Serialization[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900