0

I'm trying to return the Degree value of a user with a specific id number, the user can have more than one Degree

my c# code so far (not working) the error: System.InvalidOperationException: 'Sequence contains no matching element'

 public static string checkLicense(string cardType)
        {
            string x="";
            var values = from e in XDocument.Load(@".\data\PersonData.Xml").Elements("User").Single(c => c.Attribute("idNumber").Value == Properties.Settings.Default.idNumber).Descendants("DrivingCardLicence")
                         select e.Element("Degree").Value;

            foreach (var e in values)
            {
                x += e;
            }
            return x;
}

my XML file:

<?xml version="1.0" encoding="utf-8"?>
<Users>
  <User idNumber="666666666">
    <IdCard>
      <FirstName>Majd</FirstName>
      <LastName>sadi</LastName>
      <FatherName>asfasf</FatherName>
      <MotherName>asf</MotherName>
      <GrandFatherName>sdfgasf</GrandFatherName>
      <Sex>זכר</Sex>
      <ImagePath></ImagePath>
      <DateOfBirth>25/06/1996 18:22:48</DateOfBirth>
      <Address>asf</Address>
    </IdCard>
  </User>
  <User idNumber="999999999">
    <IdCard>
      <FirstName>asfasf</FirstName>
      <LastName>asfasf</LastName>
      <FatherName>asfasf</FatherName>
      <MotherName>asfasf</MotherName>
      <GrandFatherName>asfasf</GrandFatherName>
      <Sex>זכר</Sex>
      <ImagePath>C:\Users\m\Desktop\pictures\me\meirl.jpg</ImagePath>
      <DateOfBirth>31/03/1996 18:27:46</DateOfBirth>
      <Address>asfdasf</Address>
    </IdCard>
    <DrivingCardLicence>
      <ImagePath>C:\Users\m\Desktop\pictures\me\IMG_20200214_015706.jpg</ImagePath>
      <imageFilePath>C:\Users\m\Desktop\pictures\2.jfif</imageFilePath>
      <Degree>D1</Degree>
      <Degree>A2</Degree>
      <ReleaseDate>31/03/2020</ReleaseDate>
      <Validation>31/03/2030</Validation>
      <CardNumber>4067510</CardNumber>
    </DrivingCardLicence>
  </User>

note: not all users have DrivingCardLicence element as you can see in xml code, the id=999999999 user have a degree d1 but the id=666666666 user does not so the x should be empty.

1 Answer 1

1

You were good on your way with this.

You forgot to include the root element Users via . Element("Users")

var values = 
    from e in doc.Element("Users").Elements("User")
        .Single(c => c.Attribute("idNumber").Value == Properties.Settings.Default.idNumber)
        .Elements("DrivingCardLicence")
        .Elements("Degree")
    select e.Value;
Sign up to request clarification or add additional context in comments.

4 Comments

Actually ... you don't need it here. I updated my answer.
worked well, but when the user has more than one <Degree> it only returns the first one for example : <Degree>D1</Degree> <Degree>A2</Degree>
Can you update your question with how the xml looks like in such a case?
updated the xml code in the question, check the id=999999999

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.