0

我有以下 XML:

   <request>
       <book>
          <id>1833801</id>
          <title>The Yiddish Policemen's Union   </title> 
           <work>
               <id>1234</id>
               <name/>
           </work>
           <similar_books>
              <book><id>243859</id><title>Stations of Tide</title>                           <isbn>0380817616</isbn>
                 <authors><author><id>14454</id><name>Michael Swanwick</name></author>            </authors>
              </book>
          </similar_books>
          <authors>
              <author>
         <id>2715</id><name>Michael Chabon</name>
           <ratings_count>215884</ratings_count></author>
          </authors>
          <popular_shelves>
         <shelf name="jewish" count="104"/><shelf name="sci-fi" count="100"/>                   
         </popular_shelves>
       </book>                
     </request>

我希望所有标签都具有各自的值,并且我正在使用以下代码:

           HttpWebRequest oReq = (HttpWebRequest)WebRequest.Create(uriRoot);

                    HttpWebResponse resp = (HttpWebResponse)oReq.GetResponse();
                    log.Info(" (ISBN= " + isbn10 + ") Http request has response.");

                    if (resp.ContentType.StartsWith("application/xml", StringComparison.InvariantCultureIgnoreCase))
                    {
                        Stream resultStreamISBN = resp.GetResponseStream();
                        Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); //encoding for non-latin chars
                        StreamReader responseReader = new StreamReader(resultStreamISBN, encode);

        XDocument xdoc = XDocument.Parse(responseReader.ReadToEnd());
        var books = (from u in xdoc.Descendants().Elements("book")
                     select new
                     {

                         id = (string)u.Element("title"),
                         title = (string)u.Element("title"),               
                         works = (from i in u.Elements("work")
                                  select new
                                  {
                                      work_best_book_id = (int)i.Element("id"),
                                      work_name = (string)i.Element("name"),

                                  }).ToList(),
                          authors = (from i in u.Elements("authors").Elements("author")
                                    select new
                                    {
                                        id = (int)i.Element("id"),
                                        name = (string)i.Element("name"),                                       
                                        rating = (int)i.Element("rating_count")
                                    }).ToList(),
                         popular_shelves = (from i in u.Elements("popular_shelves").Elements("shelf")
                                            select new
                                            {
                                                name = (string)i.Attribute("name"),
                                                count = (int)i.Attribute("count")
                                            }).ToList(),

                     }).ToList();

该代码返回空值并且无法正常工作。我还应该注意,不同的 xml 文件可能没有所有标签的值。

关于如何改进我的代码的任何建议?

4

2 回答 2

0

你可以改造

id = (int)i.Element("id"),

id = (int?)i.Element("id"),

整个修改后的代码

// I am inserting the following line to demonstrate how I load the XML in test code
// Basically, I copied & pasted xml in a file OP gave named request.xml
//XDocument xdoc = XDocument.Load( @"d:\Data\request.xml");

编辑

XDocument xdoc = XDocument.Load("http://www.goodreads.com/book/isbn?isbn=0007295685&key=lbScLXWyNGQ1q0BDoFFSg");

var books = (from u in xdoc.Descendants().Elements("book")
                select new
                {

                    id = (string)u.Element("title"),
                    title = (string)u.Element("title"),
                    works = (from i in u.Elements("work")
                            select new
                            {
                                work_best_book_id = (int?)i.Element("id"),
                                work_name = (string)i.Element("name"),

                            }).ToList(),
                    authors = (from i in u.Elements("authors").Elements("author")
                            select new
                            {
                                id = (int?)i.Element("id"),
                                name = (string)i.Element("name"),
                                rating = (int?)i.Element("rating_count")
                            }).ToList(),
                    popular_shelves = (from i in u.Elements("popular_shelves").Elements("shelf")
                                    select new
                                    {
                                        name = (string)i.Attribute("name"),
                                        count = (int?)i.Attribute("count")
                                    }).ToList(),

                }).ToList();
于 2013-07-25T22:22:04.333 回答
0

检查缺少的元素,例如:

name = i.Element("name") == null ? null : i.Element("name")

对于值类型,您必须将它们更改为可为空的类型才能正常工作。

于 2013-07-25T21:55:53.260 回答