C# 如何使用LINQtoXML获取属性值?
使用上面的代码,我可以得到以下结果: 但是我需要列C# 如何使用LINQtoXML获取属性值?,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,使用上面的代码,我可以得到以下结果: 但是我需要列WorkPhone的值424-555-0545而不是Home和列HomePhone的值423-555-0124而不是Home 我应该怎么做?使用Where方法: 有关家庭电话号码: private void Window_Loaded(object sender, RoutedEventArgs e) { emplyeeDetails = XDocument.Load(Directory.GetParent(Directory.GetC
WorkPhone
的值424-555-0545
而不是Home
和列HomePhone
的值423-555-0124
而不是Home
我应该怎么做?使用
Where
方法:
有关家庭电话号码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
emplyeeDetails = XDocument.Load(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\LinqToXml\\Xmls\\" + "Employees.xml");
var emplyees = from emp in emplyeeDetails.Descendants("Employee").Take(10)
orderby emp.Element("EmpId").Value ascending
select new
{
Id = emp.Element("EmpId").Value,
Name = emp.Element("Name").Value,
Sex = emp.Element("Sex").Value,
WorkPhone=emp.Element("Phone").Attribute("Type").Value,
HomePhone = emp.Element("Phone").Attribute("Type").Value,
};
DgrdEmployeeDetails.ItemsSource = emplyees.ToList();
}
emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Home").Value
工作电话号码:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
emplyeeDetails = XDocument.Load(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\LinqToXml\\Xmls\\" + "Employees.xml");
var emplyees = from emp in emplyeeDetails.Descendants("Employee").Take(10)
orderby emp.Element("EmpId").Value ascending
select new
{
Id = emp.Element("EmpId").Value,
Name = emp.Element("Name").Value,
Sex = emp.Element("Sex").Value,
WorkPhone=emp.Element("Phone").Attribute("Type").Value,
HomePhone = emp.Element("Phone").Attribute("Type").Value,
};
DgrdEmployeeDetails.ItemsSource = emplyees.ToList();
}
emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Home").Value
是可枚举的emp.Elements(“Phone”)
的所有“Phone”元素emp
将获取满足指定属性的元素(如果有0个或多个元素满足该属性,则会引发错误)Single
是属性“Type”(即“Home”或“Work”)的值phoneElement.Attribute(“Type”)。Value
emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Work").Value
如果元素emp
可能没有工作电话或家庭电话号码,则上述代码将在单个
中引发异常。要处理此情况,您必须将代码更改为:
var emplyees = from emp in emplyeeDetails.Descendants("Employee").Take(10)
orderby emp.Element("EmpId").Value ascending
select new
{
Id = emp.Element("EmpId").Value,
Name = emp.Element("Name").Value,
Sex = emp.Element("Sex").Value,
WorkPhone = emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Home").Value,
HomePhone = emp.Elements("Phone").Single(phoneElement => phoneElement.Attribute("Type").Value == "Work").Value,
};
SingleOrDefault
将等于null
如果没有“Phone”元素满足条件,并且XElement
上的字符串
相当于XElement。Value
即使员工存在任何Phone
元素,此代码也将工作:
(string)emp.Elements("Phone").SingleOrDefault(phoneElement => phoneElement.Attribute("Type").Value == "Home")
使用强制转换元素来字符串
,int
等,而不是访问值
属性。为什么?因为如果xml中缺少某些元素或属性,则会出现NullReferenceException
。但强制转换将返回默认值。因此,上面的代码甚至可以像这样解析xml:
var emplyees =
from emp in emplyeeDetails.Descendants("Employee").Take(10)
let phones = emp.Descendants("Phone")
orderby (int)emp.Element("EmpId")
select new
{
Id = (int)emp.Element("EmpId"),
Name = (string)emp.Element("Name"),
Sex = (string)emp.Element("Sex"),
WorkPhone = (string)phones.FirstOrDefault(p => (string)p.Attribute("Type") == "Work"),
HomePhone = (string)phones.FirstOrDefault(p => (string)p.Attribute("Type") == "Home")
};
1.
山姆
423-555-0124
524-777-1234
或者,在C#6+中,您可以使用。所以它看起来像:Name=emp.Element(“Name”)?.Value代码>