如何使用 LINQ 将 XML 元素选择到强类型集合中?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的问题是“UserID”、“Username”、“Password”等不是 User 标签的属性,它们是子元素。可能想尝试这个:
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: