将属性保存并加载到xml

时间:2014-08-19 15:27:08

标签: c# xml wpf xaml

这是我的代码:

settings.xaml

        <TextBox x:Name="txtNumber" PreviewMouseDown="myKeyPad_PreviewMouseDown" Height="34" HorizontalAlignment="Left" Margin="-253,-123,0,0" VerticalAlignment="Top" Width="134" Background="{DynamicResource AccentColorBrush}" Controls:TextboxHelper.Watermark="Enter text here too...." />
        <TextBox PreviewMouseDown="myKeyPad_PreviewMouseDown" Height="34" HorizontalAlignment="Left" Margin="-114,-123,0,0" VerticalAlignment="Top" Width="134" Background="{DynamicResource AccentColorBrush}" Controls:TextboxHelper.Watermark="Enter text here too...."/>

        <TextBox x:Name="rtb1" PreviewMouseDown="myKeyPad_PreviewMouseDown" Height="34" HorizontalAlignment="Left" Margin="-253,-84,0,0" VerticalAlignment="Top" Width="134" Background="{DynamicResource AccentColorBrush}" Controls:TextboxHelper.Watermark="Enter text here too...."  />
        <TextBox PreviewMouseDown="myKeyBoard_PreviewMouseDown" Height="34" HorizontalAlignment="Left" Margin="-114,-83,0,0" VerticalAlignment="Top" Width="134" Background="{DynamicResource AccentColorBrush}" Controls:TextboxHelper.Watermark="Enter text here too...."/>
        <StackPanel Margin="0,0,0.4,0.2">
            <StackPanel Orientation="Horizontal">
                <Label Content="ID:" Margin="5" Width="50"/>
                <TextBox x:Name="txtId" Margin="5" Width="200"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="Name:" Margin="5" Width="50"/>
                <TextBox x:Name="txtName" Margin="5" Width="200"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="Age:" Margin="5" Width="50"/>
                <TextBox x:Name="txtAge" Margin="5" Width="200" Controls:TextboxHelper.Watermark="Enter text here too...."/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Label Content="Wolle:" Margin="5" Width="50"/>
                <TextBox x:Name="txtWolle" Margin="5" Width="200" Custom:TextboxHelper.Watermark="Enter Wolle here too...."/>
            </StackPanel>
            <Button Content="Save" x:Name="Save" Click="Save_Click" Width="100" Height="30" Margin="97,5,97.4,5"/>
        </StackPanel>

    </Grid>

Setting.xaml.cs显示如此:

 private void Save_Click(object sender, RoutedEventArgs e)
    {
        string id = this.txtId.Text;
        string name = this.txtName.Text;
        string age = this.txtAge.Text;
        string Wolle = this.txtWolle.Text;

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");

        //Create a root element
        XmlNode rootNode = xmlDoc.CreateElement("User");
        //Create a sub element
        XmlNode subNode = xmlDoc.CreateElement("User");
        //Create subNode's attribute
        XmlAttribute idAttribute = xmlDoc.CreateAttribute("Id");
        idAttribute.Value = id;

        XmlAttribute nameAttribute = xmlDoc.CreateAttribute("Name");
        nameAttribute.Value = name;

        XmlAttribute ageAttribute = xmlDoc.CreateAttribute("Age");
        ageAttribute.Value = age;

        XmlAttribute WolleAttribute = xmlDoc.CreateAttribute("Wolle");
        WolleAttribute.Value = Wolle;

        subNode.Attributes.Append(idAttribute);
        subNode.Attributes.Append(nameAttribute);
        subNode.Attributes.Append(ageAttribute);
        subNode.Attributes.Append(WolleAttribute);

        rootNode.AppendChild(subNode);
        xmlDoc.AppendChild(rootNode);

        xmlDoc.Save("User.xml");

        MessageBox.Show("Created Successful");
    }            

我如何在Application Start上的TextBox中读取属性?

0 个答案:

没有答案