locked
Extract Values from XML using Linq RRS feed

  • Question

  • User28706445 posted

    Hi I need help axtract and count specific values from my xml schema.

    Here is the XML file:

    <elkCareArea>
      <huntingteam id="1">
        <name>Sundbergs Jaktlag</name>
        <licensnumber>123456</licensnumber>
        <areaHA>150</areaHA>
        <hunting>
          <allocation year="2013">
            <maleElk>10</maleElk>
            <femaleElk>5</femaleElk>
            <babyElk>2</babyElk>
          </allocation>
          <discharge year="2013">
            <count>3</count>
            <elk>
              <shooter>Christer Nyström</shooter>
              <date>2013-10-15</date>
              <sex>tjur</sex>
              <whight>500</whight>
              <tags>15</tags>
              <age>5</age>
            </elk>
            <elk>
              <shooter>Christer Nyström</shooter>
              <date>2013-11-11</date>
              <sex>ko</sex>
              <wight>200</wight>
              <tags>0</tags>
              <age>3</age>
            </elk>
            <elk>
              <shooter>Gregor Von Götet</shooter>
              <date>2013-11-08</date>
              <sex>tjur</sex>
              <wight>450</wight>
              <tags>10</tags>
              <age>6</age>
            </elk>
          </discharge>
        </hunting>
        <management>
          <huntingleader id="1">
            <forename>Rickard</forename>
            <lastname>Nyström</lastname>
            <phone>+46756258426</phone>
            <adress>Brödgatan 11</adress>
            <city>Sundberg</city>
          </huntingleader>
          <hunters>
            <hunter>Christer Nyström</hunter>
            <hunter>Gustaf Håkansson</hunter>
            <hunter>Gregor Von Götet</hunter>
          </hunters>
        </management>
        <accountDetails>
          <accountName>sundbergsjaktlag</accountName>
          <password>123456</password>
        </accountDetails>
      </huntingteam>
    </elkCareArea>

    what I need to do is count the number of elks based on certain properties:

    by the elk sex and and if its an adult elk or not(not adult age = 0)

    so what I want my result to be when u load the form is that the result should look like this: 2 adult bulls, 1adult kow 0 minor kows and 0 minor bulls.

    I've been in to loops but dont now how to use the correct properties or such please help.

    Monday, November 4, 2013 6:11 AM

Answers

  • User-1965857832 posted

    Hope this helps

    var adultBullsCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "tjur" && Convert.ToInt32(node.Element("age").Value) > 0
                            select node).Count();
    
    var adultCowsCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "ko" && Convert.ToInt32(node.Element("age").Value) > 0
                            select node).Count();
    
    var minorBullsCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "tjur" && Convert.ToInt32(node.Element("age").Value) == 0
                            select node).Count();
    
    var minorCowsCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "ko" && Convert.ToInt32(node.Element("age").Value) == 0
                            select node).Count();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 4, 2013 8:09 AM

All replies

  • User-1965857832 posted

    Not too sure wat u want, but hope the below will help u to an extend

    var xDoc = XDocument.Load(@"UrXmlFilePath");
    
    var tjurElks = from node in xDoc.Descendants("elk")
                where node.Element("sex").Value == "tjur"
                select node;
    
    var tjurElksCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "tjur"
                            select node).Count();
    
    var adultElksCount = (from node in xDoc.Descendants("elk")
                            where node.Element("age").Value != "0"
                            select node).Count();

    If this is not wat u want, please eloberate ur requirement a bit.

    Monday, November 4, 2013 7:18 AM
  • User28706445 posted

    Sorry if my requirements wasn't that good eloberated. But u got gave me a pretty good answere but not quite what I wanted thoe. I want the program to count if it is an adult kow ot if its an adult bull. and if it's a minor cow or a minor bull.

    so the program should only count an adult bull if the xml data is <sex>tjur</sex> and <age>(bigger then 0)</age> and an Adult cow if the data is <sex>ko</sex> and <age>(bigger then 0)</age>

    and the minor elks is the same but the age is 0

    I hope my reply makes sense :)

    Monday, November 4, 2013 7:48 AM
  • User-1965857832 posted

    Hope this helps

    var adultBullsCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "tjur" && Convert.ToInt32(node.Element("age").Value) > 0
                            select node).Count();
    
    var adultCowsCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "ko" && Convert.ToInt32(node.Element("age").Value) > 0
                            select node).Count();
    
    var minorBullsCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "tjur" && Convert.ToInt32(node.Element("age").Value) == 0
                            select node).Count();
    
    var minorCowsCount = (from node in xDoc.Descendants("elk")
                            where node.Element("sex").Value == "ko" && Convert.ToInt32(node.Element("age").Value) == 0
                            select node).Count();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 4, 2013 8:09 AM
  • User28706445 posted

    I think I solved what I was looking for using your code but I added another line:

    var tjurElksCount = (from node in details.Descendants("elk")
                                     where node.Element("sex").Value == "tjur"                               
         >>>>added>>>   where node.Element("age").Value != "0"                                
                                     select node).Count();

    so thanks for the help

    Monday, November 4, 2013 8:09 AM
  • User28706445 posted

    I'll take a look at what you just posted thx very much!

    Monday, November 4, 2013 8:10 AM