从磁盘读取/写入中间值

时间:2010-07-09 21:46:46

标签: c# .net wpf xml oop

我目前正在开发这个应用程序(C#,WPF,LINQ2XML,.NET 4.0),它显示了“动物”的详细信息。这方面的一个例子是显示动物类型的应用程序,如“鸟类”,“哺乳动物”,“爬行动物”。对于每种类型的动物,我都会有一些子条目,比如'Birds',我会有像'Sparrow','Hawk'这样的子条目。对于'爬行动物',我会有'鳄鱼','蟒蛇'等等。等等。用户可以选择为每个子条目添加注释。一个例子是子条目'Sparrow',我将有一个选项'添加评论'。点击“添加评论”将弹出一个文本框,用户可以在其中输入他或她对“麻雀”的评论。用户可以为子条目插入多个注释。用户可以对“麻雀”发表1,2,3 ...... n条评论。这只需要多次点击“添加评论”。

数据预先加载了存储在XML文件中的信息。我为每个Animal都有单独的XML,例如Birds.xml,Mammals.xml,Reptiles.xml。这些中的每一个都将具有关于子条目的信息。 Birds.xml将为'Sparrow'和'Hawk'提供XML片段。

我正在使用Linq2Xml来提取填充动物对象(模型)所需的信息,而动物对象又被绑定到Presenter(我正在使用Model-View-Presenter模式),它被提供给XAML。

我能够成功地在视图中显示XML中的信息。问题在于“评论”。

我需要在这里完成两件事:

  1. 将添加的所有注释写入单独的XML文件(AnimalComments.xml),我可以存储为所有子条目添加的所有注释。

  2. 如果应用程序失败,我希望这个相同的XML文件(AnimalComments.xml)预先填充我在应用程序崩溃之前输入的所有注释。

  3. 本质上我试图将状态序列化到磁盘上,但实际上我不希望整个对象图在磁盘上持续存在,因为我认为我要添加写入/添加到AnimalComments的功能。每当为子条目键入注释并且焦点更改为某个其他子条目时,xml文件。

    我想知道SO的集体智慧是否可以帮助我在这里添加一些优雅的设计指导,以便累积所有添加的注释并将它们保存到磁盘上,以便应用程序可以在以后用于恢复或以人类可读的格式呈现。任何需要使用DBMS的解​​决方案在这种情况下都不相关,因为它是在本地文件系统上存储与应用程序相关的任何数据的要求的一部分。

    正如Drake在下面提到的那样,有一些方法可以使用某种主键来维护动物的引用,但我感兴趣的是如何从面向对象的角度解决这个问题,或者是否有一些突出的模式可以解决这种问题一种情境。

    由于

2 个答案:

答案 0 :(得分:1)

在我看来,您的问题只是我们如何组织数据结构的问题。那么让我们来看看每个问题:

1

  

写下添加到的所有评论   单独的XML文件(AnimalComments.xml)   我可以存储所有评论   已为所有人添加   子条目

我建议在animals.xml文件中为每只动物添加一个唯一的ID字段,在animals文件中我们使用ID作为外键将注释引回动物。

例如,我们有像这样的birds.xml

    <?xml version="1.0" encoding="utf-8"?>
<Animals>
  <Animal ID="1">
    <Name>Jack Sparrow</Name>
  </Animal>
  <Animal ID="2">
    <Name>Black Hawk</Name>
  </Animal >
</Animals>

然后animalcomments.xml文件应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<Animals>
  <Animal ID="1">
    <Comments>
      <Comment CommentID="1">
        <Content>Hello world</Content>
      </Comment>
      <Comment CommentID="2">
        <Content>Hello world 2</Content>
      </Comment>
    </Comments>
  </Animal>
  <Animal ID="2">
    <Comments>
      <Comment CommentID="1">
        <Content>Hello world</Content>
      </Comment>
      <Comment CommentID="2">
        <Content>Hello world 2</Content>
      </Comment>
    </Comments>
  </Animal>  
</Animals>

因此,您可以基于该数据结构生成代码,读取和写入animalcomments.xml文件的每个元素。

  

2

     

如果应用程序失败,我想要这个相同的XML   文件(AnimalComments.xml)来   预先填写我的所有评论   在我的申请崩溃之前输入。

在你的

  

HandleUnhandledException()

方法,在终止程序之前保存所有注释。重新加载程序后,您可以收到所有评论。

希望得到这个帮助。

答案 1 :(得分:1)

我在下面创建了这些类。请复制代码并传递给编辑器!

  

动物。

using System.Xml.Serialization;



/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/XMLSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)]
public partial class Animals {

    private Animal[] animalField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Animal")]
    public Animal[] Animal {
        get {
            return this.animalField;
        }
        set {
            this.animalField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/XMLSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)]
public partial class Animal {

    private string nameField;

    private int idField;

    private bool idFieldSpecified;

    /// <remarks/>
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int ID {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool IDSpecified {
        get {
            return this.idFieldSpecified;
        }
        set {
            this.idFieldSpecified = value;
        }
    }
}
  

AnimalComments.cs

using System.Xml.Serialization;




/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/AnimalComments.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/AnimalComments.xsd", IsNullable=false)]
public partial class Animals {

    private Animal[] animalField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Animal")]
    public Animal[] Animal {
        get {
            return this.animalField;
        }
        set {
            this.animalField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/AnimalComments.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/AnimalComments.xsd", IsNullable=false)]
public partial class Animal {

    private Comments commentsField;

    private int idField;

    private bool idFieldSpecified;

    /// <remarks/>
    public Comments Comments {
        get {
            return this.commentsField;
        }
        set {
            this.commentsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int ID {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool IDSpecified {
        get {
            return this.idFieldSpecified;
        }
        set {
            this.idFieldSpecified = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/AnimalComments.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/AnimalComments.xsd", IsNullable=false)]
public partial class Comments {

    private Comment[] commentField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Comment")]
    public Comment[] Comment {
        get {
            return this.commentField;
        }
        set {
            this.commentField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/AnimalComments.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/AnimalComments.xsd", IsNullable=false)]
public partial class Comment {

    private string contentField;

    private int commentIDField;

    private bool commentIDFieldSpecified;

    /// <remarks/>
    public string Content {
        get {
            return this.contentField;
        }
        set {
            this.contentField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int CommentID {
        get {
            return this.commentIDField;
        }
        set {
            this.commentIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool CommentIDSpecified {
        get {
            return this.commentIDFieldSpecified;
        }
        set {
            this.commentIDFieldSpecified = value;
        }
    }
}

干杯。

相关问题