如何从属性中捕获值

时间:2014-08-06 13:37:13

标签: c# xml linq-to-xml

我有这样简单的XML文件结构:

  <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>

以及该XML文件中的更多Employees

我只想选择两个值:NamePhone,其属性为Type = Home

var query = from nm in xElement.Elements("Employee")
            orderby nm.Element("EmpId") descending
            select new 
            {
              Name = nm.Element("Name").Value,
              work_phone = ((string)nm.Element("Phone").Attribute("Type") == "Home").ToString().Single()
        };

但它不起作用。问题出在work_phone(关于IComparable的例外情况)。我怎样才能得到这个值?

5 个答案:

答案 0 :(得分:2)

您可以尝试这种方式:

work_phone = (string)nm.Elements("Phone")
                       .FirstOrDefault(o => (string)o.Attribute("Type") == "Home");

另一个问题出在orderby子句中。尝试将<EmpId>值转换为int以使其具有可比性:

.....
orderby (int)nm.Element("EmpId") descending
.....

答案 1 :(得分:2)

以下是两个选定的值:

var query = from nm in xElement.Elements("Employee")
                        orderby nm.Element("EmpId") descending
                        select new
                        {
                            Name = nm.Element("Name").Value,
                            HomePhone = (string)nm.Elements("Phone").SingleOrDefault(y => (string)y.Attribute("Type") == "Home")
                        };

答案 2 :(得分:1)

var query = from nm in xElement.Elements("Employee")
            orderby (int)nm.Element("EmpId") descending
            select new
            {
                Name = nm.Element("Name").Value,
                work_phone = nm.Elements("Phone").First(p=>p.Attribute("Type").Value=="Home").Value
            };

答案 3 :(得分:0)

如此处所述: - http://www.dotnetcurry.com/showarticle.aspx?ID=564

XElement xelement = XElement.Load("..\\..\\Employees.xml");

var homePhone = from phoneno in xelement.Elements("Employee")

                where (string)phoneno.Element("Phone").Attribute("Type") == "Home"

                select phoneno;

Console.WriteLine("List HomePhone Nos.");

foreach (XElement xEle in homePhone)

{

    Console.WriteLine(xEle.Element("Phone").Value);

}

答案 4 :(得分:0)

我们走了:

var xml_data = "<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>";
            var xdoc = XDocument.Parse(xml_data);

            var result = xdoc.Elements("Employee").
                Where((x) =>
                    {
                        var home_phones = x.Elements("Phone").Where(y => (string)y.Attribute("Type") == "Home").ToList();
                        return home_phones.Count > 0;
                    }).
                ToList();