web-dev-qa-db-ja.com

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

しかし、何をしようとしても、「画像」の子ノードを読み取ることができませんでした。

12
k787

<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"));
        }
    }
}
26
carlosfigueira