开发者

LINQ To XML Syntax and class objects in vb.net

开发者 https://www.devze.com 2023-01-30 12:12 出处:网络
I have the following XML file <T开发者_开发百科est> <Modules> <Module Name= \"Test1\">

I have the following XML file

<T开发者_开发百科est>
  <Modules>
    <Module Name= "Test1">
        <QueueName Name="Test1Active1" Active="True"></QueueName>
        <QueueName Name="Test1Active2" Active="True"></QueueName>
        <QueueName Name="Test1Active3" Active="True"></QueueName>
        <QueueName Name="Test1Active4" Active="True"></QueueName>
        <QueueName Name="Test1Inactive1" Active="False"></QueueName>
        <QueueName Name="Test1Inactive2" Active="False"></QueueName>
        <QueueName Name="Test1Inactive3" Active="False"></QueueName>
        <QueueName Name="Test1Inactive4" Active="False"></QueueName>

      <OtherSettings TimePause="20" TimeElasped="25" NumberOfThreads="5">

        <Connection tag="Test1Test1" Connection="TestCon1"></Connection>
        <Connection tag="Test1Test2" Connection="TestCon2"></Connection>
        <Url Tag="Test1Test1" Connection="TestUrl"></Url>
        <Url Tag="Test1Test2" Connection="TestUrl1"></Url>

      </OtherSettings> 
    </Module>
    <Module Name= "Test2">

        <QueueName Name="Test2Active1" Active="True"></QueueName>
        <QueueName Name="Test2Active2" Active="True"></QueueName>
        <QueueName Name="Test2Active3" Active="True"></QueueName>
        <QueueName Name="Test2Active4" Active="True"></QueueName>
        <QueueName Name="Test2Inactive1" Active="False"></QueueName>
        <QueueName Name="Test2Inactive2" Active="False"></QueueName>
        <QueueName Name="Test2Inactive3" Active="False"></QueueName>
        <QueueName Name="Test2Inactive4" Active="False"></QueueName>


      <OtherSettings>
         <Connection Tag="Test2Test1" Connection="TestCon1"></Connection>
         <Connection Tag="Test2Test2" Connection="TestCon2"></Connection>
         <Url Tag="Test2Test1" Connection="TestUrl"></Url>
         <Url Tag="Test2Test2" Connection="TestUrl1"></Url>
      </OtherSettings>
   </Module>
  </Modules>
</Test>

I have tried to am trying to fill classes from this

Partial Public Class Modules
    Private _moduleName As String
    Private _queueName As IEnumerable(Of QueueName)
    Private _otherSetting As OtherSetting

    Public Sub New()

    End Sub
    <DebuggerBrowsable(DebuggerBrowsableState.Never)> _
    Public Property ModuleName() As String
        Get
            Return _moduleName
        End Get
        Set(ByVal value As String)
            _moduleName = value
        End Set
    End Property
    <DebuggerBrowsable(DebuggerBrowsableState.Never)> _
    Public Property QueueName() As IEnumerable(Of QueueName)
        Get
            Return _queueName

        End Get
        Set(ByVal value As IEnumerable(Of QueueName))
            _queueName = value

        End Set
    End Property
    <DebuggerBrowsable(DebuggerBrowsableState.Never)> _
    Public Property OtherSetting() As OtherSetting
        Get
            Return _otherSetting

        End Get
        Set(ByVal value As OtherSetting)
            _otherSetting = value

        End Set
    End Property

End Class

These work

  Friend Function GetDataModules() As List(Of Modules)
        Dim xdoc As XDocument = OpenXml()

        Dim modules = (From q In xdoc...<Modules>.<Module> _
                    Select New Modules With { _
                    .ModuleName = q.@Name _
                    }).ToList


        Return modules

    End Function
    Friend Function GetDataQueueNames(ByVal ModuleName As String, ByVal isActive As Boolean) As List(Of QueueName)
        Dim xdoc As XDocument = OpenXml()

        Dim active = (From q In xdoc...<Modules>.<Module> _
                      Where q.@Name = ModuleName _
                      From l In q.<QueueName> _
                      Where Convert.ToBoolean(l.@Active) = isActive _
                      Select New QueueName With { _
                        .Name = l.@Name, _
                        .Active = Convert.ToBoolean(l.@Active) _
                        }).ToList




        Return active
    End Function

This does not

Protected Function GetAllData() As List(Of Modules)

    Dim xdoc As XDocument = OpenXml()
    Dim All = From m In xdoc...<Module>.<Module> _
         Select New Modules With { _
         .ModuleName = m.@Name _
         }, _
         .QueueName = From q In m...<QueueName> _
         Select New QueueName With { _
         .Name = q.@Name, _
         .Active = Convert.ToBoolean(q.@Active) _
         }, _
         .OtherSetting = From o In m...<OtherSettings> _
         Select New OtherSetting() With { _
         .TimeElasped = Convert.ToInt32(o.@TimeElasped), _
         .TimePause = Convert.ToInt32(o.@TimePause), _
         .NumberOfThreads = Convert.ToInt32(o.@NumberOfThreads) _
          }, _
         .Connection = From c In m...<Connection> _
         Select New Connection With { _
         .ConnectionString = c.@Connection, _
         .Tag = c.@Tag _
         }, _
        .URL = From u In m...<Url> _
        Select New URL() With { _
        .Tag = u.@Tag, _
        .URL = u.@Url _
       }.tolist
End Function

Could you please help


I think you have put the braces at the wrong position i.e. if you change

Dim All = From m In xdoc...<Module>.<Module> _
     Select New Modules With { _
     .ModuleName = m.@Name _
     }, _
     .QueueName = From q In m...<QueueName> _
     Select New QueueName With { _
     .Name = q.@Name, _
     .Active = Convert.ToBoolean(q.@Active) _
     }, _
     ...

to

Dim All = From m In xdoc...<Module>.<Module> _
     Select New Modules With { _
     .ModuleName = m.@Name _
     , _
     .QueueName = From q In m...<QueueName> _
     Select New QueueName With { _
     .Name = q.@Name, _
     .Active = Convert.ToBoolean(q.@Active) _
     }, _
     ...
     }

then you have the correct type construction.

0

精彩评论

暂无评论...
验证码 换一张
取 消