vb.net 中的 LINQ To XML 语法和类对象

发布于 2024-10-07 11:24:38 字数 5518 浏览 3 评论 0原文

我有以下 XML 文件,

<Test>
  <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>

我试图从这个文件中填充类

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

这些工作

  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

这不是

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

你能帮忙吗

I have the following XML file

<Test>
  <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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野生奥特曼 2024-10-14 11:24:38

我认为你把大括号放在了错误的位置,即如果你改变了

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) _
     }, _
     ...

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) _
     }, _
     ...
     }

那么你就有了正确的类型结构。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文