2009-12-21 121 views
1

我有以下xml相同的子元素(Filed),我想从每个Child元素获取值。从Xml查找具有相同元素名称的值

Sameple XML

<root xmlns=""> 
    <books cat="F1" ISBN="01F187597" genre="Programming"> 
     <Field name="Title" val="XML" /> 
     <Field name="Publish Date" val="20010424" /> 
     <Field name="Price" val="43.00" /> 
    </books> 
</root> 

代码

XDocument xdoc = XDocument.Load("c:\\test6.xml"); 

    var booksData = from book in xdoc.Descendants("root") 
        //I guess create this and do something with it 
        // let fieldElements = book.Descendants("Field") 
        select new Book 
        { 
         cat = book.Element("books").Attribute("cat").Value 
         ,ISBN = book.Element("books").Attribute("ISBN").Value 
         ,genre = book.Element("books").Attribute("genre").Value 
         ,Price = "?" 
         ,PublishDate="?" 
         ,Title="?" 
        }; 

图书类

public class Book 
    { 
    public string cat {get;set;} 
    public string ISBN {get;set;} 
    public string genre {get;set;} 
    public string Title {get;set;} 
    public string PublishDate {get;set;} 
    public string Price { get; set; } 
    } 

回答

0

我与丹尼斯xdoc.Descendants(“根”)同意并没有真正做任何事情。

我修改代码,以便它会检查attrbiute存在与否也可以如何使用lambda表达式。

var booksData = from book in xdoc.Descendants("books") 
          select new Book 
          { 
           cat = book.Attribute("cat").Value, 
           ISBN = book.Attribute("ISBN").Value, 
           genre = book.Attribute("genre").Value, 

           //Dennis Code 

           Price = (from childField in book.Elements() 
             where childField.Attribute("name").Value == "Price" 
             select childField.Attribute("val").Value).SingleOrDefault(), 

           //same as Dennis code but using lambda expression 

           PublishDate = book.Elements("Field").Where(p => p.Attribute("name").Value == "Publish Date") 
               .Select(p => p.Attribute("val").Value).SingleOrDefault(), 

           // Check if element exists or not 
           //if not exists it will insert empty string 

           Title = book.Elements("Field").Any(p => p.Attribute("name").Value == "Title") 
             ? book.Elements("Field").Where(p=> p.Attribute("name").Value == "Title") 
              .Select(p => p.Attribute("val")== null 
                ? string.Empty 
                : p.Attribute("val").Value 
               ).Single() 
             : string.Empty        }; 
+0

任何人有更好的办法了相同的代码,那么请张贴 – NETQuestion 2009-12-23 00:15:18

0

这会给你你在找什么。当然,如果这些属性中的任何一个都不可能总是存在,那么在调用它的Value属性之前,您需要添加一些空值检查以确保每个属性都存在。

一旦在查询中有<books>元素,就可以执行子查询来选择所需的特定<Field>元素。

var booksData = from book in xdoc.Descendants("books") 
       select new Book 
          { 
           cat = book.Attribute("cat").Value, 
           ISBN = book.Attribute("ISBN").Value, 
           genre = book.Attribute("genre").Value, 
           Price = (from childField in book.Elements() 
             where childField.Attribute("name").Value == "Price" 
             select childField.Attribute("val").Value).SingleOrDefault(), 
           PublishDate = (from childField in book.Elements() 
               where childField.Attribute("name").Value == "Publish Date" 
               select childField.Attribute("val").Value).SingleOrDefault(), 
           Title = (from childField in book.Elements() 
             where childField.Attribute("name").Value == "Title" 
             select childField.Attribute("val").Value).SingleOrDefault() 
          }; 

关于您在问题中发布的代码的一个注释。方法xdoc.Descendants("root")没有做任何事情。 from book in xdoc会做同样的事情。我在回答中使用了Descendants("books"),这正是我认为你无论如何应该做的。

0
string xml = @"<root xmlns=''> <books cat='F1' ISBN='01F187597' genre='Programming'>  <Field name='Title' val='XML' />  <Field name='Publish Date' val='20010424' />  <Field name='Price' val='43.00' />  </books> </root>"; 



      XDocument xdoc = XDocument.Parse(xml); 

      var booksData = from book in xdoc.Descendants("root") 
          //I guess create this and do something with it 
          // let fieldElements = book.Descendants("Field") 
          select new Book 
          { 
           cat = book.Element("books").Attribute("cat").Value 
            , ISBN = book.Element("books").Attribute("ISBN").Value 
            , genre = book.Element("books").Attribute("genre").Value 
            , Price = book.Elements("books").Elements("Field").Single(b => b.Attribute("name").Value == "Price").Attribute("val").Value 
            , PublishDate = book.Elements("books").Elements("Field").Single(b => b.Attribute("name").Value == "Publish Date").Attribute("val").Value 
            , Title = book.Elements("books").Elements("Field").Single(b => b.Attribute("name").Value == "Title").Attribute("val").Value 
          }; 

     } 


     public class Book 
     { 
      public string cat { get; set; } 
      public string ISBN { get; set; } 
      public string genre { get; set; } 
      public string Title { get; set; } 
      public string PublishDate { get; set; } 
      public string Price { get; set; } 
     } 

我觉得这是我会怎么做。

+0

你的代码将无法在以下情况下 工作,如果属性val为例如失踪 <字段名称= “标题”/> OR Title元素缺少 <根的xmlns = “”> <书籍猫= “F1” ISBN = “01F187597” 流派= “幻想”> <字段名称=”发布日期“val =”20010424“/> <字段名称=”价格“val =”43“/> – NETQuestion 2009-12-23 00:56:13

+0

您提出一个好的观点。谢谢 – 2009-12-23 01:22:11

相关问题