Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML LINQ VB.NET
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.
Posted 8-Apr-13 12:15pm
Edited 8-Apr-13 12:17pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Steve Van Lint at 9-Apr-13 13:07pm
   
Thanks man. Works like a charm.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100