在 Linq to XML 中将 XML 属性转换为字典
我有一个程序需要将特定标记的两个属性转换为 Dictionary
的键和值。 XML 看起来像这样:(
片段)
<startingPoint coordinates="1,1" player="1" />
到目前为止,我的 LINQ 看起来像这样:
XNamespace ns = "http://the_namespace";
var startingpoints = from sp in xml.Elements(ns+"startingPoint")
from el in sp.Attributes()
select el.Value;
这为我提供了一个很好的 IEnumerable
,其中充满了“1,1”和“1”之类的内容,但应该有一种适应这个答案之类的方法来处理属性而不是元素的方法。请帮忙一点?谢谢你!
I've got a program that needs to convert two attributes of a particular tag to the key and value of an Dictionary
. The XML looks like this:<int,string>
(fragment)
<startingPoint coordinates="1,1" player="1" />
and so far my LINQ looks something like this:
XNamespace ns = "http://the_namespace";
var startingpoints = from sp in xml.Elements(ns+"startingPoint")
from el in sp.Attributes()
select el.Value;
Which gets me a nice IEnumerable
full of things like "1,1" and "1", but there should be a way to adapt something like this answer to do attributes instead of elements. Little help please? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您想在字典中存储所有玩家的映射及其各自的起点坐标。代码如下所示:
更好的是,您有一个用于存储坐标的类,例如:
然后您可以立即将字符串解析为坐标:
然后您可以像这样访问玩家坐标:
I assume you want to store a mapping of all players and their respective starting point coordinates in the dictionary. The code for this would look like:
Even better, you had a class for storing the coordinates, like:
Then you could parse the string into coordinates right away:
You could then access the players coordinates like this:
我想你想做这样的事情
I think you're looking to do something like this