Linq to Xml C# 在特定元素中查找特定元素
Linq to Xml C# find specific element inside specific element
我正在尝试 return 具有特定 ID 号的用户的 Degree 值,该用户可以拥有多个 Degree
到目前为止我的 C# 代码(不工作)
错误: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;
}
我的XML文件:
<?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.jfif</imageFilePath>
<Degree>D1</Degree>
<Degree>A2</Degree>
<ReleaseDate>31/03/2020</ReleaseDate>
<Validation>31/03/2030</Validation>
<CardNumber>4067510</CardNumber>
</DrivingCardLicence>
</User>
注意:并非所有用户都有 DrivingCardLicence 元素
正如您在 xml 代码中看到的,id=999999999 用户的学位为 d1
但是 id=666666666 用户没有,所以 x 应该是空的。
你在这方面做得很好。
您忘记通过 包含根元素 Users
。 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;
我正在尝试 return 具有特定 ID 号的用户的 Degree 值,该用户可以拥有多个 Degree
到目前为止我的 C# 代码(不工作) 错误: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;
}
我的XML文件:
<?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.jfif</imageFilePath>
<Degree>D1</Degree>
<Degree>A2</Degree>
<ReleaseDate>31/03/2020</ReleaseDate>
<Validation>31/03/2030</Validation>
<CardNumber>4067510</CardNumber>
</DrivingCardLicence>
</User>
注意:并非所有用户都有 DrivingCardLicence 元素 正如您在 xml 代码中看到的,id=999999999 用户的学位为 d1 但是 id=666666666 用户没有,所以 x 应该是空的。
你在这方面做得很好。
您忘记通过 包含根元素 Users
。 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;