2

我想解决方案很简单,但是尽管在互联网上进行了广泛搜索,但我还没有找到答案。请帮忙!问题是:如果所需的属性不在类似命名元素的第一个元素内,我的 xml 查询不会返回所需的结果。例如,此代码不返回任何内容:

XElement xelement = XElement.Load("~/Employees.xml"));
        var homePhone = from phoneno in xelement.Elements("Employee")
               where (string)phoneno.Element("Phone").Attribute("Type") == "Work"
        select phoneno;
foreach (XElement xEle in homePhone)
        TextBox1.Text= xEle + "/n";

但是,如果我用“家”代替“工作”,我会得到所需的结果。事实上,我应该得到与“工作”完全相同的结果。有人可以解释如何做到这一点吗?

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>
  <Employee>
    <EmpId>2</EmpId>
    <Name>Lucy</Name>
    <Sex>Female</Sex>
    <Phone Type="Other">143-555-0763</Phone>
    <Phone Type="Work">434-555-0567</Phone>
    <Address>
      <Street>Jess Bay</Street>
      <City>Alta</City>
      <State>CA</State>
      <Zip>95701</Zip>
      <Country>USA</Country>
    </Address>
  </Employee>
  <Employee>
    <EmpId>3</EmpId>
    <Name>Kate</Name>
    <Sex>Female</Sex>
    <Phone Type="Home">166-555-0231</Phone>
    <Phone Type="Other">233-555-0442</Phone>
    <Address>
      <Street>23 Boxen Street</Street>
      <City>Milford</City>
      <State>CA</State>
      <Zip>96121</Zip>
      <Country>USA</Country>
    </Address>
  </Employee>
  <Employee>
    <EmpId>4</EmpId>
    <Name>Chris</Name>
    <Sex>Male</Sex>
    <Phone Type="Work">564-555-0122</Phone>
    <Phone Type="Other">442-555-0154</Phone>
    <Address>
      <Street>124 Kutbay</Street>
      <City>Montara</City>
      <State>CA</State>
      <Zip>94037</Zip>
      <Country>USA</Country>
    </Address>
  </Employee>
</Employees>
4

2 回答 2

0

这一行:

where (string)phoneno.Element("Phone")

确实只使用了第一个 Phone 元素,因为您只请求了一个。您需要使用该Elements方法,但您可以迭代所有电话而不是员工:

from phone in xelement.Descendants("Phone")
where (string)phone.Attribute("Type") == "Work"
select phone;

如果您想确保此电话属于员工,请在以下位置添加此条件where

phone.Parent.Name == "Employee"
于 2012-11-01T14:34:19.677 回答
0

当存在多个时,XElement.Element("name") 将返回给定名称的第一个元素。你真的想要这个:

var employeesWithHomePhone = from e in xdocument.Root.Elements("Employee")
                             where e.Elements("Phone").Any( p => (string)p.Attribute("Type") == "Home") 
                             select e;

只需将“家庭”更改为“工作”即可获得具有工作号码的人

于 2012-11-01T14:34:33.067 回答