从xml文件中删除选定的节点

时间:2019-06-01 01:10:09

标签: c# .net xml wpf

我正在尝试为我的课程制作一些音乐库应用。我已经完成了几乎所有需要做的事情,但是还剩下一件事。我不知道如何访问数据模板生成的项目中的数据,以从XML文件中删除此项目表单应用程序和xml节点。

我发现这段代码应该删除我想要的节点,但是我仍然不知道如何从我选择的项目中访问数据。

private void DeleteFromFile_Click(object sender, RoutedEventArgs e)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(@"musicLibrary.xml");
        foreach(XmlNode xNode in xDoc.SelectNodes("musicLibrary"))
        {
            if(xNode.SelectSingleNode("title").InnerText == "")
            {
                xNode.ParentNode.RemoveChild(xNode);
            }
        }
    }

节点看起来像这样:

  <musicLibrary>
<title>I Still Haven't Found What I'm Looking For</title>
<albumName>The Joshua Tree</albumName>
<artist>U2</artist>
<releaseDate>1987</releaseDate>
<tempo>101</tempo>
<duration>4:37</duration>
<genre>Rock</genre>
  </musicLibrary>
  <musicLibrary>
<title>Magnificent</title>
<albumName>No Line on the Horizon</albumName>
<artist>U2</artist>
<releaseDate>2009</releaseDate>
<tempo>115</tempo>
<duration>5:24</duration>
<genre>Rock</genre>
  </musicLibrary>

我使用的数据模板:

<Page.Resources>
    <CollectionViewSource x:Key="listDataView"/>
    <DataTemplate x:Key="mDataTamplate">
        <StackPanel>
            <TextBlock FontSize="12" Margin="5" Text="{Binding title}" />
            <TextBlock FontSize="10" Margin="5" Text="{Binding artist}"/>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="detailedDataTamplate">
        <Grid  Grid.Row="3" Margin="20,0,20,0">
            <!-- Row/Column Definition-->
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.7*"/>
                <ColumnDefinition Width="1.5*"/>
                <ColumnDefinition Width="0.7*"/>
                <ColumnDefinition Width="0.7*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <!-- First Column -->
            <TextBlock Grid.Column="0" Grid.Row="0" Text="Title" Style="{StaticResource textBlockStyle}" />
            <TextBlock         Text="{Binding title}"
                               Grid.Column="1" Grid.Row="0"  
                               Style="{StaticResource textBlockStyle}"/>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="Album name" Style="{StaticResource textBlockStyle}"/>
            <TextBlock         Text="{Binding albumName}" 
                               Grid.Column="1" Grid.Row="1"  
                               Style="{StaticResource textBlockStyle}"/>
            <TextBlock Grid.Column="0" Grid.Row="2" Text="Artist" Style="{StaticResource textBlockStyle}"/>
            <TextBlock         Text="{Binding artist}"  
                               Grid.Column="1" Grid.Row="2"  
                               Style="{StaticResource textBlockStyle}"/>
            <TextBlock Grid.Column="0" Grid.Row="3" Text="Release Date" Style="{StaticResource textBlockStyle}"/>
            <TextBlock         Text="{Binding releaseDate}"   
                               Grid.Column="1" Grid.Row="3"  
                               Style="{StaticResource textBlockStyle}"/>
            <!-- Second Column -->
            <TextBlock Grid.Column="2" Grid.Row="0" Text="Tempo" Style="{StaticResource textBlockStyle}" />
            <TextBlock Text="{Binding tempo}" 
                                Grid.Column="3" Grid.Row="0"
                                Style="{StaticResource textBlockStyle}"/>
            <TextBlock Grid.Column="2" Grid.Row="1" Text="Duration" Style="{StaticResource textBlockStyle}"/>
            <TextBlock Grid.Column="3" Grid.Row="1" 
                       Text="{Binding duration}" 
                       Style="{StaticResource textBlockStyle}"/>
            <TextBlock Grid.Column="2" Grid.Row="2" Text="Genre" Style="{StaticResource textBlockStyle}"/>
            <TextBlock Grid.Column="3" Grid.Row="2" 
                       Text="{Binding genre}" 
                       Style="{StaticResource textBlockStyle}"/>

            <Button x:Name="saveToFile" Click="saveToFile_Click" Content="Save to file"  Grid.Column="2" Grid.Row="3" Grid.ColumnSpan="1" VerticalAlignment="Center"  Height="31"/>
            <Button x:Name="deleteFromFile" Click="DeleteFromFile_Click" Content="Delete" Grid.Column="3" Grid.Row="3"/>
        </Grid>
    </DataTemplate>
</Page.Resources>

我绑定数据来自的类:

namespace MLUI
 {
[XmlRoot("dataSet")]
public class DataSet
{
    [XmlElement("musicLibrary")]
    public List<Song> Songs { get; set; }
}
public class Song
{
    [XmlElement("title")]
    public string title { get; set; }
    [XmlElement("artist")]
    public string artist { get; set; }
    [XmlElement("albumName")]
    public string albumName { get; set; }
    [XmlElement("releaseDate")]
    public string releaseDate { get; set; }
    [XmlElement("tempo")]
    public int tempo { get; set; }        
    [XmlElement("duration")]
    public string duration { get; set; }
    [XmlElement("genre")]
    public string genre { get; set; }
    public override string ToString()
    {
        return "Title:  " + title +"\r\nArtist:  "+ artist +"\r\nAlbum Name:  "+ albumName + "\r\nRelease Date:  " + releaseDate +
            "\r\nTempo  "+ tempo + "\r\nDuration  " + duration + "\r\nGenre  " + genre ;
    }
}
}

1 个答案:

答案 0 :(得分:0)

我喜欢将Xml Linq与字典配合使用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement dataSet = doc.Root;

            Dictionary<string, XElement> dictionary = dataSet.Elements("musicLibrary")
                .GroupBy(x => (string)x.Element("title"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            XElement Magnificent = dictionary["Magnificent"];

            Magnificent.Remove();
        }
    }
}