如何使用 LINQ 将 XML 元素选择到强类型集合中?

发布于 2024-09-02 23:14:18 字数 1956 浏览 0 评论 0原文

我正在尝试使用 LINQ 作为处理 XML 数据的方法,但在找出将元素选择到强类型集合中的最佳方法时遇到了问题。我确信我遗漏了一些细节,但我似乎看不到它。

以下是我的 XML 文件的示例:

    <users>
      <User>
        <UserId>a3ba555e-9dbe-4f5e-a8b0-56be91a99eae</UserId>
        <Password>FCC2427123934BC2A06297847574A50E</Password>
        <Username>TestTEst</Username>
        <FirstName>Test</FirstName>
        <LastName>TestTEst</LastName>
      </User>
      <User>
        <UserId>500629d5-c22a-4585-bc85-330391f44fac</UserId>
        <Password>FCC2427123934BC2A06297847574A50E</Password>
        <Username>TestTEst</Username>
        <FirstName>Test</FirstName>
        <LastName>TestTEst</LastName>
      </User>
      <User>
        <UserId>bd6b78db-9cd7-403b-a757-a8c013bdc523</UserId>
        <Password>FCC2427123934BC2A06297847574A50E</Password>
        <Username>TestTEst</Username>
        <FirstName>Test</FirstName>
        <LastName>TestTEst</LastName>
      </User>
</users>

当我使用以下代码创建对象时:

Dim userList = From u In users.Descendants("user") _
               Select u

那么 userList 将包含我的 XML 文件的元素。如果我尝试将 XML 文件选择到强类型集合中:

Dim userList = (From u In users.Descendants("user") _
               Select New User With { _
                   .UserId = New Guid(u.Attribute("UserId").Value), _
                   .Username = u.Attribute("Username").Value, _
                   .Password = u.Attribute("Password").Value, _
                   .FirstName = u.Attribute("FirstName").Value, _
                   .LastName = u.Attribute("LastName").Value _
               }).ToList()

则 userList 为空。无论我是否显式指定 userList (List(Of User)) 的类型,这都是相同的。我做错了什么?直接从 XML 创建强类型集合的正确方法是什么?

谢谢,

迈克

I am trying to wrap my brain around using LINQ as a means of working with XML data and I'm having a problem figuring out the best way to select elements into a strongly-typed collection. I'm sure there's some minute detail I'm missing, but I can't seem to see it.

Here is a sample of my XML File:

    <users>
      <User>
        <UserId>a3ba555e-9dbe-4f5e-a8b0-56be91a99eae</UserId>
        <Password>FCC2427123934BC2A06297847574A50E</Password>
        <Username>TestTEst</Username>
        <FirstName>Test</FirstName>
        <LastName>TestTEst</LastName>
      </User>
      <User>
        <UserId>500629d5-c22a-4585-bc85-330391f44fac</UserId>
        <Password>FCC2427123934BC2A06297847574A50E</Password>
        <Username>TestTEst</Username>
        <FirstName>Test</FirstName>
        <LastName>TestTEst</LastName>
      </User>
      <User>
        <UserId>bd6b78db-9cd7-403b-a757-a8c013bdc523</UserId>
        <Password>FCC2427123934BC2A06297847574A50E</Password>
        <Username>TestTEst</Username>
        <FirstName>Test</FirstName>
        <LastName>TestTEst</LastName>
      </User>
</users>

When I create an object using the following code:

Dim userList = From u In users.Descendants("user") _
               Select u

Then userList will contain the elements of my XML file. If I try to select the XML file into a strongly-typed collection instead:

Dim userList = (From u In users.Descendants("user") _
               Select New User With { _
                   .UserId = New Guid(u.Attribute("UserId").Value), _
                   .Username = u.Attribute("Username").Value, _
                   .Password = u.Attribute("Password").Value, _
                   .FirstName = u.Attribute("FirstName").Value, _
                   .LastName = u.Attribute("LastName").Value _
               }).ToList()

Then userList is empty. This is the same whether or not I explicitly specify the type of userList (List(Of User)). What am I doing wrong? What is the correct way to create a strongly-typed collection directly from XML?

Thanks,

Mike

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

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

发布评论

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

评论(1

梦断已成空 2024-09-09 23:14:18

我认为你的问题是“UserID”、“Username”、“Password”等不是 User 标签的属性,它们是子元素。可能想尝试这个:

Dim userList = (From u In users.Descendants("user") _
               Select New User With { _
                   .UserId = New Guid(u.Decendants("UserId").First().Value), _
                   .Username = u.Decendants("Username").First().Value, _
                   .Password = u.Decendants("Password").First().Value, _
                   .FirstName = u.Decendants("FirstName").First().Value, _
                   .LastName = u.Decendants("LastName").First().Value _
               }).ToList()

I think your problem here is that "UserID", "Username", "Password", etc. are not attributes of the User tag, they are sub-elements. Might want to try this:

Dim userList = (From u In users.Descendants("user") _
               Select New User With { _
                   .UserId = New Guid(u.Decendants("UserId").First().Value), _
                   .Username = u.Decendants("Username").First().Value, _
                   .Password = u.Decendants("Password").First().Value, _
                   .FirstName = u.Decendants("FirstName").First().Value, _
                   .LastName = u.Decendants("LastName").First().Value _
               }).ToList()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文