C#XMLreader和子节点

C#XMLreader和子节点,第1张

概述我有这个xml文件. <movies> <movie> <score>8.582207</score> <popularity>3</popularity> <translated>true</translated> <adult>false</adul 我有这个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#XMLreader和子节点全部内容,希望文章能够帮你解决C#XMLreader和子节点所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1247154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存