当我运行时,当我尝试写出XML文件时,我得到一个无效操作异常

本文关键字:无效 异常 操作 一个 运行时 文件 XML | 更新日期: 2023-09-27 18:33:52

我有一个父类 PartNumber 和两个子类 PartNumberChild1 和 PartNumberChild2。 在我的主要创建一个 PartNumberContainer 并添加两个子 1 对象和两个子 2 对象,我想将不同的子类存储在 PartNumberContainer 类的 PartNumber 列表中,并以 XML 格式写入和读取 PartNumberContainer。

当我

运行时,当我尝试写出XML文件时,我得到一个无效操作异常。

[Serializable]
[XmlInclude(typeof(PartNumberChild1))]
[XmlInclude(typeof(PartNumberChild2))]
public class PartNumber
{
  private string partNumber = "";
  public abstract ClassType;
  public PartNumber()
  {
  }
  public PartNumber(string partNod)
  {
    partNumber = partNo;
  }
  public string PartNumber
  {
    get { return partNumber; }
    set { partNumber = value; }
  }
}
[Serializable]
Public class PartNumberChild1 : PartNumber
{
  private string subPartNumber;
  private string classType = "Child1";
  public PartNumberChild1(string partNo, string subPartNo)
  :base(partNo)
  {
    subPartNumber = subPartNo;
  }
  public PartNumberChild1()
  {
  }
  public string SubPartNumber
  {
    get { return subPartNumber; }
    set { subPartNumber = value; }
  }
  public string ClassType
  {
    get { return classType; }
    set { classType = value; }
  }
}
[Serializable]
Public class PartNumberChild2 : PartNumber
{
  private string subPartNumber;
  private string classType = "Child2";
  public PartNumberChild1(string partNo, string subPartNo)
  :base(partNo)
  {
    subPartNumber = subPartNo;
  }
  public PartNumberChild2()
  {
  }
  public string SubPartNumber
 {
    get { return subPartNumber; }
    set { subPartNumber = value; }
 }
 public string ClassType
 {
    get { return classType; }
    set { classType = value; }
 }
}
[Serializable]
public class PartNumberContainer
{
  public static string PART_NUMBER_FILENAME_NAME = " Part_Numbers.xml";
  private List<PartNumber> partNumbers;
  public PartNumberContainer()
  {
    partNumbers = new List<PartNumber>();
  }
  public List<PartNumber> PartNumbers
  {
    get { return partNumbers; }
    set { partNumbers = value; }
  }
  public void AddPartNumber(PartNumber partNo)
  {
    partNumbers.Add(partNo);
  }
  public void AddPartNumber(string partNo)
  {
    partNumbers.Add(new PartNumber(partNo));
  }
  public void WriteMeToFile()
   {
    try
    {

我收到此行的错误。

      XmlSerializer x = new XmlSerializer(this.GetType());
      StreamWriter writer = new StreamWriter(PART_NUMBER_FILENAME_NAME);
        writer.Flush();
        x.Serialize(writer, this);
        writer.Close();
    }
    catch (IOException)
    {
       MessageBox.Show("Exception", "Error Message",       MessageBoxButton.OK, MessageBoxImage.Warning);
    }
}
public static PartNumberContainer ReadMeFromFile()
{
  PartNumberContainer fileData = new PartNumberContainer();
  object myObj = fileData;
  try
  {
    XMLFileReaderWriter.ReadConfigurationFile(ref myObj, GetConfigurationFileFolderPath(), PART_NUMBER_FILENAME_NAME);
  try
  {
      XmlSerializer x = new XmlSerializer(myObj.GetType());
      StreamReader reader = new StreamReader(PART_NUMBER_FILENAME_NAME);
      myProperties = x.Deserialize(reader);
  }
  catch
  {
       MessageBox.Show("Exception", "Error Message", MessageBoxButton.OK, MessageBoxImage.Warning);
  }
    fileData = (PartNumberContainer)myObj;
  }
  catch
  {  // Error reading file
    MessageBox.Show(err, "Error Message", MessageBoxButton.OK, MessageBoxImage.Warning);
  }
  return fileData;
}
public MainWindow()
{
public static PartNumberContainer myPartNoContainer = new UUTPartNumberContainer();
  myPartNoContainer.AddPartNumber(new PartNumberChild1("onePartNo","OneSubPartNo"));
  myPartNoContainer.AddPartNumber(new PartNumberChild1("twoPartNo","twoSubPartNo"));
  myPartNoContainer.AddPartNumber(new PartNumberChild2("onePartNo","OneSubPartNo"));
  myPartNoContainer.AddPartNumber(new PartNumberChild2("twoPartNo","twoSubPartNo"));
  myPartNoContainer.WriteMeToFile();

 PartNumberContainer myPartNoContainerRead = PartNumberContainer.ReadMeFromFile();
 Foreach(PartNumber partNo in myPartNoContainerRead. PartNumbers)
{
  WriteLine(string.format(“The Part No = {0}, The subPartNumber = {1},     

the  ClassType = {2} “,partNo.PartNumber, partNo. SubPartNumber , partNo. ClassType )
}

当我运行时,当我尝试写出XML文件时,我得到一个无效操作异常

我所要做的就是让两个子类都公开 -- 公共类 PartNumberChild2 : PartNumber 我必须添加构造函数公共 PartNumberChild2() { }