C# C XML反序列化为多个对象

C# C XML反序列化为多个对象,c#,xml,C#,Xml,我正在将一个给定的XML文件反序列化为一个对象,这个对象工作得很好。但是,我现在需要修改XML文件,以便生成额外的属性,这些属性应该反序列化到一个单独的对象中,以便从一个给定的XML文件中可以填充两个对象,这在一个操作中可能吗 以下是我当前的代码和XML: XML的现状: <NameCollection> <Names> <Name> <description></description>

我正在将一个给定的XML文件反序列化为一个对象,这个对象工作得很好。但是,我现在需要修改XML文件,以便生成额外的属性,这些属性应该反序列化到一个单独的对象中,以便从一个给定的XML文件中可以填充两个对象,这在一个操作中可能吗

以下是我当前的代码和XML:

XML的现状:

<NameCollection>
   <Names>
      <Name>
        <description></description>
      <Name>
      <Name>
        <description></description>
      <Name>
   </Names>
</NameCollection>
以及当前对象:

[Serializable()]
public class TestCommand
{
   public string description{get;set;}
}

[Serializable()]
[XmlRoot("NameCollection")]
public class NameCollection
{
   [XmlArray("Commands")]
   [XmlArrayItem("Command", typeof(TestCommand))]
   public TestCommand[] TestCommand {get;set;}
}

然后,我希望将GenericName和GenericDescription属性添加到另一个单独的对象,这就是我一直坚持的方向。

我认为您的目标是让您的类反映XML的结构,而不是两个类。所以你想要一个像这样的结构:

public class TestCommand
{
   public string description{get;set;}
}

[XmlRoot("NameCollection")]
public class NameCollection
{
    public string GenericName {get; set;}
    public string GenericDescription {get; set;}

   [XmlArray("Commands")]
   [XmlArrayItem("Command", typeof(TestCommand))]
   public TestCommand[] TestCommand {get;set;}
}

然后以完全相同的方式序列化它,工作完成。

我认为您的目标是让类反映XML的结构,而不是两个类。所以你想要一个像这样的结构:

public class TestCommand
{
   public string description{get;set;}
}

[XmlRoot("NameCollection")]
public class NameCollection
{
    public string GenericName {get; set;}
    public string GenericDescription {get; set;}

   [XmlArray("Commands")]
   [XmlArrayItem("Command", typeof(TestCommand))]
   public TestCommand[] TestCommand {get;set;}
}

然后以完全相同的方式对其进行序列化,工作完成。

使用您现有的布局,XmlSerializer唯一希望放置这些额外值的位置是NameCollection,即


如果您想让它在其他对象上运行,那么:XmlSerializer不会这样做。

按照您现有的布局,XmlSerializer只想将这些额外值放在NameCollection上,即


如果您想让它在其他对象上运行,那么:XmlSerializer不会这样做。

您所说的另一个单独的对象是什么意思,以及它与NameCollection的关系如何?如果我们假设Name/Names===Command/Commands,那么这里的一切都应该已经起作用了;您应该只需要将公共字符串GenericName{get;set;}和公共字符串GenericDescription{get;set;}添加到NameCollection…?TestCommand对象存储描述属性。我想要一个新的对象来保存和保存属性,但是我不确定如何才能实现。作为旁注:XmlSerializer不需要[Serializable]。你所说的另一个单独的对象是什么意思,以及它与NameCollection的关系如何?如果我们假设Name/Names==Command/Commands,那么这里的所有内容都应该已经工作了;您应该只需要将公共字符串GenericName{get;set;}和公共字符串GenericDescription{get;set;}添加到NameCollection…?TestCommand对象存储描述属性。我想要一个新的对象来保存和保存属性,但是我不确定如何才能实现。作为旁注:XmlSerializer不需要[Serializable]。@Liam我想你早30秒就说过了,但是:有时候从两个独立的来源听到同样的事情两次是值得的,作为交叉验证,他们没有编造。@Liam我想你提前30秒说出来,但是:有时候,从两个独立的来源听到同样的事情两次是值得的,因为交叉验证,他们没有编造。
public class TestCommand
{
   public string description{get;set;}
}

[XmlRoot("NameCollection")]
public class NameCollection
{
    public string GenericName {get; set;}
    public string GenericDescription {get; set;}

   [XmlArray("Commands")]
   [XmlArrayItem("Command", typeof(TestCommand))]
   public TestCommand[] TestCommand {get;set;}
}
[XmlRoot("NameCollection")]
public class NameCollection
{
    public string GenericName {get;set:}
    public string GenericDescription {get;set:}

    [XmlArray("Names")]
    [XmlArrayItem("Name", typeof(TestCommand))]
    public TestCommand[] TestCommand {get;set;}
}