C#XMLreader和子节点

我有这个xml文件.
<movies>
             <movie>
               <score>8.582207</score>
               <popularity>3</popularity>
               <translated>true</translated>
               <adult>false</adult>
               <language>en</language>
               <original_name>Transformers</original_name>
               <name>Transformers</name>
               <alternative_name>The Transformers</alternative_name>
               <type>movie</type>
               <id>1858</id>
               <imdb_id>tt0418279</imdb_id>
               <url>http://www.themoviedb.org/movie/1858<;/url>
               <Votes>28</Votes>
               <rating>7.2</rating>
               <certification>PG-13</certification>
               <overview>The Earth is caught in the middle of an intergalactic war /overview>
               <released>2007-07-04</released>
               <images>
                    <image type="poster" url="http://hwcdn.themoviedb.org/posters/304/4bc91347017a3c57fe007304/transformers-original.jpg" size="original" id="4bc91347017a3c57fe007304"/>
                    <image type="poster" url="http://hwcdn.themoviedb.org/posters/304/4bc91347017a3c57fe007304/transformers-mid.jpg" size="mid" id="4bc91347017a3c57fe007304"/>
                    <image type="poster" url="http://hwcdn.themoviedb.org/posters/304/4bc91347017a3c57fe007304/transformers-cover.jpg" size="cover" id="4bc91347017a3c57fe007304"/>
                    <image type="poster" url="http://hwcdn.themoviedb.org/posters/304/4bc91347017a3c57fe007304/transformers-thumb.jpg" size="thumb" id="4bc91347017a3c57fe007304"/>
                    <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/2ce/4bc91339017a3c57fe0072ce/transformers-original.jpg" size="original" id="4bc9133s9017a3c57fe0072ce"/>
                    <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/2ce/4bc91339017a3c57fe0072ce/transformers-poster.jpg" size="poster" id="4bc91339017a3c57fe0072ce"/>
                    <image type="backdrop" url="http://hwcdn.themoviedb.org/backdrops/2ce/4bc91339017a3c57fe0072ce/transformers-thumb.jpg" size="thumb" id="4bc91339017a3c57fe0072ce"/>
               </images>
             <last_modified_at>2010-04-26 03:26:14</last_modified_at>
       </movie>
     </movies>

我正在阅读所有的属性使用

textReader.ReadToFollowing("original_name");
string title =textReader.ReadElementContentAsstring("original_name",textReader.NamespaceURI);

然而,无论我尝试,我无法设法读取“图像”的子节点.

解决方法

您应该阅读< images>元素,然后读取到第一个< image>后代,然后读到下一个兄弟姐妹,直到你不能再.下面的代码显示了如何做到这一点.
public class StackOverflow_6473251
{
    public static void test()
    {
        string xml = @"               <movies>
             <movie>
               <score>8.582207</score>
               <popularity>3</popularity>
               <translated>true</translated>
               <adult>false</adult>
               <language>en</language>
               <original_name>Transformers</original_name>
               <name>Transformers</name>
               <alternative_name>The Transformers</alternative_name>
               <type>movie</type>
               <id>1858</id>
               <imdb_id>tt0418279</imdb_id>
               <url>http://www.themoviedb.org/movie/1858</url>
               <Votes>28</Votes>
               <rating>7.2</rating>
               <certification>PG-13</certification>
               <overview>The Earth is caught in the middle of an intergalactic war /overview>
               <released>2007-07-04</released>
               <images>
                    <image type=""poster"" url=""http://hwcdn.themoviedb.org/posters/304/4bc91347017a3c57fe007304/transformers-original.jpg"" size=""original"" id=""4bc91347017a3c57fe007304""/>
                    <image type=""poster"" url=""http://hwcdn.themoviedb.org/posters/304/4bc91347017a3c57fe007304/transformers-mid.jpg"" size=""mid"" id=""4bc91347017a3c57fe007304""/>
                    <image type=""poster"" url=""http://hwcdn.themoviedb.org/posters/304/4bc91347017a3c57fe007304/transformers-cover.jpg"" size=""cover"" id=""4bc91347017a3c57fe007304""/>
                    <image type=""poster"" url=""http://hwcdn.themoviedb.org/posters/304/4bc91347017a3c57fe007304/transformers-thumb.jpg"" size=""thumb"" id=""4bc91347017a3c57fe007304""/>
                    <image type=""backdrop"" url=""http://hwcdn.themoviedb.org/backdrops/2ce/4bc91339017a3c57fe0072ce/transformers-original.jpg"" size=""original"" id=""4bc9133s9017a3c57fe0072ce""/>
                    <image type=""backdrop"" url=""http://hwcdn.themoviedb.org/backdrops/2ce/4bc91339017a3c57fe0072ce/transformers-poster.jpg"" size=""poster"" id=""4bc91339017a3c57fe0072ce""/>
                    <image type=""backdrop"" url=""http://hwcdn.themoviedb.org/backdrops/2ce/4bc91339017a3c57fe0072ce/transformers-thumb.jpg"" size=""thumb"" id=""4bc91339017a3c57fe0072ce""/>
               </images>
             <last_modified_at>2010-04-26 03:26:14</last_modified_at>
       </movie>
     </movies>";
        XmlReader r = XmlReader.Create(new StringReader(xml));
        r.ReadToFollowing("original_name");
        string title = r.ReadElementContentAsstring("original_name",r.NamespaceURI);
        r.ReadToFollowing("images");
        int imageCount = 0;
        if (r.ReadToDescendant("image"))
        {
            do
            {
                Console.WriteLine("Image {0}",++imageCount);
                Console.WriteLine("  Type: {0}",r.GetAttribute("type"));
                Console.WriteLine("  URL: {0}",r.GetAttribute("url"));
                Console.WriteLine("  Size: {0}",r.GetAttribute("size"));
                Console.WriteLine("  ID: {0}",r.GetAttribute("id"));
            } while (r.ReadToNextSibling("image"));
        }
    }
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...