Linq 2 XML 不返回基于属性值的元素
Linq 2 XML not returning element based in attribute value
我有以下 XML...
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<EmpId>1</EmpId>
<Name>Sam</Name>
<Sex>Male</Sex>
<Phone Type="Home">423-555-0124</Phone>
<Phone Type="Work">424-555-0545</Phone>
<Address>
<Street>7A Cox Street</Street>
<City>Acampo</City>
<State>CA</State>
<Zip>95220</Zip>
<Country>USA</Country>
</Address>
<Employee>
<EmpId>5</EmpId>
<Name>Kenneth Lowrey</Name>
<Sex>Male</Sex>
<Phone Type="Home">555-555-3477</Phone>
<Phone Type="Work">444-123-2557</Phone>
<Address>
<Street>6026 Amberwoods Drive</Street>
<City>Boca Raton</City>
<State>FL</State>
<Zip>33433</Zip>
<Country>USA</Country>
</Address>
</Employee>
</Employee>
</Employees>
我有以下控制台应用程序代码...
using System;
using System.Xml.Linq;
using System.Linq;
class Program
{
public static void Main(String[] args)
{
String strPath = @"C:\XMLExample\Employees.xml";
XElement xEle = XElement.Load(strPath);
var empquery = from e in xEle.Descendants("Employee")
select new
{
name = e.Element("Name").Value,
homephone = (string)e.Element("Phone").Attribute("Type").Value=="Home" ? e.Element("Phone").Value : "",
workphone = (string)e.Element("Phone").Attribute("Type").Value=="Work" ? e.Element("Phone").Value : "",
};
foreach (var e in empquery)
{
Console.WriteLine("{0}'s home phone is {1} work phone is {2}", e.name, e.homephone, e.workphone);
}
Console.WriteLine("Press <enter> to continue");
Console.ReadLine();
}
}
我试图在我的查询表达式中区分家庭和工作 phone 号码。
不过我只能拿到家里的phone号码..
我做错了什么?
Element()
方法 returns 仅匹配指定名称的第一个元素。因此,对于每个 Employee
,您只会获得第一个 phone(主页)。
更好的方法是使用 Elements()
方法获取与指定名称匹配的所有元素,然后按属性 Type
值过滤:
var empquery = from e in xEle.Descendants("Employee")
select new
{
name = e.Element("Name").Value,
homephone = (string)e.Elements("Phone").Where(x => x.Attribute("Type").Value == "Home").FirstOrDefault().Value,
workphone = (string)e.Elements("Phone").Where(x => x.Attribute("Type").Value == "Work").FirstOrDefault().Value
};
我有以下 XML...
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<EmpId>1</EmpId>
<Name>Sam</Name>
<Sex>Male</Sex>
<Phone Type="Home">423-555-0124</Phone>
<Phone Type="Work">424-555-0545</Phone>
<Address>
<Street>7A Cox Street</Street>
<City>Acampo</City>
<State>CA</State>
<Zip>95220</Zip>
<Country>USA</Country>
</Address>
<Employee>
<EmpId>5</EmpId>
<Name>Kenneth Lowrey</Name>
<Sex>Male</Sex>
<Phone Type="Home">555-555-3477</Phone>
<Phone Type="Work">444-123-2557</Phone>
<Address>
<Street>6026 Amberwoods Drive</Street>
<City>Boca Raton</City>
<State>FL</State>
<Zip>33433</Zip>
<Country>USA</Country>
</Address>
</Employee>
</Employee>
</Employees>
我有以下控制台应用程序代码...
using System;
using System.Xml.Linq;
using System.Linq;
class Program
{
public static void Main(String[] args)
{
String strPath = @"C:\XMLExample\Employees.xml";
XElement xEle = XElement.Load(strPath);
var empquery = from e in xEle.Descendants("Employee")
select new
{
name = e.Element("Name").Value,
homephone = (string)e.Element("Phone").Attribute("Type").Value=="Home" ? e.Element("Phone").Value : "",
workphone = (string)e.Element("Phone").Attribute("Type").Value=="Work" ? e.Element("Phone").Value : "",
};
foreach (var e in empquery)
{
Console.WriteLine("{0}'s home phone is {1} work phone is {2}", e.name, e.homephone, e.workphone);
}
Console.WriteLine("Press <enter> to continue");
Console.ReadLine();
}
}
我试图在我的查询表达式中区分家庭和工作 phone 号码。
不过我只能拿到家里的phone号码..
我做错了什么?
Element()
方法 returns 仅匹配指定名称的第一个元素。因此,对于每个 Employee
,您只会获得第一个 phone(主页)。
更好的方法是使用 Elements()
方法获取与指定名称匹配的所有元素,然后按属性 Type
值过滤:
var empquery = from e in xEle.Descendants("Employee")
select new
{
name = e.Element("Name").Value,
homephone = (string)e.Elements("Phone").Where(x => x.Attribute("Type").Value == "Home").FirstOrDefault().Value,
workphone = (string)e.Elements("Phone").Where(x => x.Attribute("Type").Value == "Work").FirstOrDefault().Value
};