无法读取简单的xml文件

时间:2011-12-23 15:35:51

标签: c# .net xml xml-parsing

我知道这看起来像一个愚蠢的简单问题,但无论如何我会问它。我一直在尝试阅读这个xml文件:

<Songs>
<song>
  <TrackID>1666</TrackID>
  <Name>What's the Matter Here?</Name>
  <Artist>10,000 Maniacs</Artist>
  <AlbumArtist>10,000 Maniacs</AlbumArtist>
  <Composer>Natalie Merchant/Robert Buck</Composer>
  <Album>In My Tribe</Album>
  <Genre>Rock</Genre>
  <Kind>MPEG audio file</Kind>
  <Size>9318485</Size>
  <TotalTime>291134</TotalTime>
  <TrackNumber>1</TrackNumber>
  <Year>1987</Year>
  <DateModified>2005-03-09T07:31:09Z</DateModified>
  <DateAdded>2007-07-20T17:21:36Z</DateAdded>
  <BitRate>256</BitRate>
  <SampleRate>44100</SampleRate>
  <Comments> </Comments>
  <PersistentID>54F22391EB807F38</PersistentID>
  <TrackType>File</TrackType>
  <Location></Location>      
</song>
<song>
  <TrackID>1666</TrackID>
  <Name>What's the Matter Here?</Name>
  <Artist>10,000 Maniacs</Artist>
  <AlbumArtist>10,000 Maniacs</AlbumArtist>
  <Composer>Natalie Merchant/Robert Buck</Composer>
  <Album>In My Tribe</Album>
  <Genre>Rock</Genre>
  <Kind>MPEG audio file</Kind>
  <Size>9318485</Size>
  <TotalTime>291134</TotalTime>
  <TrackNumber>1</TrackNumber>
  <Year>1987</Year>
  <DateModified>2005-03-09T07:31:09Z</DateModified>
  <DateAdded>2007-07-20T17:21:36Z</DateAdded>
  <BitRate>256</BitRate>
  <SampleRate>44100</SampleRate>
  <Comments> </Comments>
  <PersistentID>54F22391EB807F38</PersistentID>
  <TrackType>File</TrackType>
  <Location></Location>      
</song>
</Songs>

我正在使用此代码阅读上述内容:

    private static void LoadSongsFromITunes(string xmlFile)
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreComments = true;
        settings.IgnoreWhitespace = true;
        string album=null ;
        string artists=null ;
        string genres=null ;
        string  year=null ;
        string duration=null ;

        try
        {
            using (XmlReader reader = XmlReader.Create(xmlFile, settings))
            {
                string xmlContent;

                while (reader.Read())
                {
                    if (reader.NodeType != XmlNodeType.Element) continue;
                    xmlContent = "";
                    string name=null;
                    if (reader.Name == "Name")
                    {
                        name = reader.ReadString().ToString();
                    }
                    if (reader.Name == "Artist")
                    {
                        artists = reader.ReadString().ToString();
                    }
                    if (reader.Name == "Album")
                    {
                        album = reader.ReadString().ToString();
                    }
                    if (reader.Name == "Genre")
                    {
                        genres = reader.ReadString().ToString();
                    }
                    if (reader.Name == "Year")
                    {
                        year = reader.ReadString();
                    }
                    if (reader.Name == "Duration")
                    {
                        duration  = reader.ReadString().ToString();
                    }
                    Console.WriteLine(name);
                }
            }
        }
        catch
        {

        }

然而,似乎读者只返回null。我通过调试器调试了代码,但我不能完全理解这段代码的错误。 P.S:有人能提供更好的方法吗?比如使用链接到xml或什么?

1 个答案:

答案 0 :(得分:6)

LINQ to XML:

var doc = XDocument.Parse(xml);

var result = doc
    .Root.Elements("song")
    .Select(e => 
        new { Name = (string)e.Element("Name"), Artist = (string)e.Element("Artist") });

foreach (var val in result)
{
    Console.WriteLine(val);
}