不能读取一个简单的xml文件

本文关键字:简单 一个 xml 文件 不能读 读取 不能 | 更新日期: 2023-09-27 18:18:46

我知道这看起来像是一个愚蠢的简单问题,但我还是要问。我一直在尝试阅读这个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
        {
        }

然而,reader似乎只是返回null。我已经通过调试器跳过了代码,但我不太明白这段代码有什么问题。附:有人能提供更好的方法吗?比如使用链接到xml之类的?

不能读取一个简单的xml文件

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