如何在LINQ to xml for vb.net中使用别名 [英] How to use alias in LINQ to xml for vb.net

查看:92
本文介绍了如何在LINQ to xml for vb.net中使用别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的第一年期末考试编程/分析师做一个刽子手游戏。



我基本上已经设置了一切所以它有效但有一个我无法完成的事情。



我的游戏将为每个玩家创建一个xml,我将根据所选难度存储每个难度/失去的统计数据。



XML示例:



  <?  xml     version   =  1.0   编码  =  utf-16    >  
< UserStats >
< 统计信息 难度 = 简单 >
< 赢了 > 10 < /赢得 >
< Lost > 0 < / Lost >
< / Stats >
< Stats 难度 = >
< 赢了 > 10 < /赢得 >
< Lost > 0 < / Lost >
< / Stats >
< 统计数据 难度 = >
< 赢了 > 10 < ; /赢得 >
< 丢失 > 0 < ; / Lost >
< / Stats >
< / UserStats >





通过执行以下操作,我能够在元素和属性上查询XML:



< pre lang =vb> Dim query As IEnumerable( XElement)=来自q UserXML。< Stats> _
其中q。@难度=差_
选择 q





如果我输出query.value,我得到元素Won和Lost的2个值,但它们是连接的。



例如,如果我这样做这个关于难度的查询容易然后我得到结果= 100



有人可以请告诉我如何将Won和Lost的值分配给现有变量以便我可以使用他们之后呢?



我可以将别名用于选择值,但我不确定如何使用它们来查询查询。

解决方案

这似乎有效:

 '  Xml。 
Dim myXml =<?xml version = 1.0编码ing = utf-16?>
< UserStats>
< Stats Difficulty = Easy>
< Won> 10 < / 赢了 >
< Lost> 0 < / Lost >
< / 统计资料 >
< Stats Difficulty = 介质>
< Won> 10 < / 赢了 >
< Lost> 0 < / Lost >
< / 统计资料 >
< Stats Difficulty = >
< Won> 10 < / 赢了 >
< Lost> 0 < / Lost >
< / 统计资料 >
< / UserStats >


' 查询。
Dim query =
来自q In myXml。< UserStats>。< Stats>
其中q。@ Difficulty = 简单
选择 使用 {
.Won = q。 < Won> .Value,
.Lost = q。< Lost> .Value}


' 第一个结果。
Dim firstItem = query.FirstOrDefault()


' 赢/输价值。
Dim 赢得作为 整数 = 整数 .Parse(firstItem.Won)
Dim lost As 整数 = 整数 .Parse(firstItem.Lost)



我使用内联XML,但想法相同。


I''m working on a hangman game for my 1st years final exam programming/analyst.

I basically have everything setup so it works but there is one thing that I am unable to accomplish.

My game will create an xml for each player where I will store that persons Won/Lost stats per difficulty selected.

XML example:

<?xml version="1.0" encoding="utf-16" ?>
<UserStats>
  <Stats Difficulty = "Easy">
    <Won>10</Won>
    <Lost>0</Lost>
  </Stats>
  <Stats Difficulty = "Medium">
    <Won>10</Won>
    <Lost>0</Lost>
  </Stats>
  <Stats Difficulty = "Hard">
    <Won>10</Won>
    <Lost>0</Lost>
  </Stats>
</UserStats>



I was able to query the XML on element and attribute by doing the following:

Dim query As IEnumerable(Of XElement) = From q In UserXML.<Stats> _
                                                Where q.@Difficulty = Diff _
                                                Select q



If I output query.value I get the 2 values of the elements Won and Lost but they are concatenated.

For example, If I do this query on difficulty Easy then I get as outcome = 100

Could someone please tell me how I can assign the values of Won and Lost to existing variables so I can use them afterwards?

I can assing aliases to the select values but I''m unsure how to use them oustide the query.

解决方案

This seems to work:

' Xml.
Dim myXml = <?xml version="1.0" encoding="utf-16"?>
            <UserStats>
                <Stats Difficulty="Easy">
                    <Won>10</Won>
                    <Lost>0</Lost>
                </Stats>
                <Stats Difficulty="Medium">
                    <Won>10</Won>
                    <Lost>0</Lost>
                </Stats>
                <Stats Difficulty="Hard">
                    <Won>10</Won>
                    <Lost>0</Lost>
                </Stats>
            </UserStats>


' Query.
Dim query =
    From q In myXml.<UserStats>.<Stats>
    Where q.@Difficulty = "Easy"
    Select New With {
        .Won = q.<Won>.Value,
        .Lost = q.<Lost>.Value}


' First result.
Dim firstItem = query.FirstOrDefault()


' Won/lost values.
Dim won As Integer = Integer.Parse(firstItem.Won)
Dim lost As Integer = Integer.Parse(firstItem.Lost)


I used inline XML, but same idea.


这篇关于如何在LINQ to xml for vb.net中使用别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆