使用XmlRoot声明将类序列化为XML问题

时间:2022-06-22 15:52:49

Im serializing my class to XML. I have an issue with the root element of one of my classes is NOT being named properly.

我将我的类序列化为XML。我有一个问题,我的一个类的根元素没有被正确命名。

The complete XML structure should look like the following.

完整的XML结构应如下所示。

<Workflow>
  <Name>My Workflow</Name>
  <Description />
  <Modules>
    <Module Name="Intro" MenuText="IntroText" />
  </Modules>
</Workflow>

However Im getting this result

但是我得到了这个结果

<Workflow>
  <Name>My Workflow</Name>
  <Description />
  <Modules>
    <WorkflowModule Name="Intro" MenuText="IntroText" />
  </Modules>
</Workflow>

I want the element "WorkflowModule" to be called "Module" however the problem is that I already have another class called Module. So to get around this problem I called it a WorkflowModule and put a class XmlRoot() declartion like so;

我希望元素“WorkflowModule”被称为“模块”,但问题是我已经有了另一个名为Module的类。因此,为了解决这个问题,我将其称为WorkflowModule,并将类XmlRoot()声明为这样;

[XmlRoot("Module")]
public class WorkflowModule
{...}

But when I serialize the Workflow class it still comes up with WorkflowModule.

但是当我序列化Workflow类时,它仍然会出现WorkflowModule。

Here are my 2 classes classes;

这是我的2班课;

[XmlRoot("Workflow")]
public class Workflow
{

    private string _name;
    private string _description;
    private List<WorkflowModule> _modules = new List<WorkflowModule>();



    [XmlElement("Name")]
    public String Name
    {
        get {  }
        set {  }
    }


    [XmlElement("Description")]
    public String Description
    {
        get {  }
        set {  }
    }


    [XmlArrayItem(typeof(WorkflowModule))]
    public List<WorkflowModule> Modules
    {
        get { }
        set { }
    }
}








[XmlRoot("Module")]
public class WorkflowModule
{

    private string _name;
    private string _menu_text;


    public WorkflowModule()
    {
    }


    [XmlAttribute("Name")]
    public String Name
    {
        get { }
        set { }

    }


    [XmlAttribute("MenuText")]
    public String MenuText
    {
        get { }
        set { }

    }

}

}

}

2 个解决方案

#1


2  

Set the element name within XmlArrayItem attrubute:

在XmlArrayItem attrubute中设置元素名称:

[XmlArrayItem(typeof(WorkflowModule), ElementName = "Module")]

#2


1  

There are many ways to control it as defined in this duplicate post How do I Set XmlArrayItem Element name for a List<Custom> implementation?

有很多方法可以控制它,如本重复帖子中所定义的如何为List 实现设置XmlArrayItem元素名称?

These attribute control serialize from the this object's perspective as it transverses nested objects

这些属性控件在遍历嵌套对象时从此对象的角度进行序列化

[XmlArray("RootArrayElementNameGoesHere")]
[XmlArrayItem(typeof(Workflow), ElementName="ArrayItemElementNameGoesHere")]
public List<WorkflowModule> Modules

This attribute redefining the element name but can be overwritten with the local [XmlArrayItem] or [XmlElement] attributes to provides local override from the owning objects serialization

此属性重新定义元素名称,但可以使用本地[XmlArrayItem]或[XmlElement]属性覆盖,以提供来自拥有对象序列化的本地覆盖

[XmlType(TypeName = "UseThisElementNameInsteadOfClassName")]
public class WorkflowModule

This attribute is only honored when its the direct object being serialized

只有在序列化直接对象时才会使用此属性

[XmlRoot("UseThisElementNameWhenItIsTheRoot")]
public class WorkflowModule

#1


2  

Set the element name within XmlArrayItem attrubute:

在XmlArrayItem attrubute中设置元素名称:

[XmlArrayItem(typeof(WorkflowModule), ElementName = "Module")]

#2


1  

There are many ways to control it as defined in this duplicate post How do I Set XmlArrayItem Element name for a List<Custom> implementation?

有很多方法可以控制它,如本重复帖子中所定义的如何为List 实现设置XmlArrayItem元素名称?

These attribute control serialize from the this object's perspective as it transverses nested objects

这些属性控件在遍历嵌套对象时从此对象的角度进行序列化

[XmlArray("RootArrayElementNameGoesHere")]
[XmlArrayItem(typeof(Workflow), ElementName="ArrayItemElementNameGoesHere")]
public List<WorkflowModule> Modules

This attribute redefining the element name but can be overwritten with the local [XmlArrayItem] or [XmlElement] attributes to provides local override from the owning objects serialization

此属性重新定义元素名称,但可以使用本地[XmlArrayItem]或[XmlElement]属性覆盖,以提供来自拥有对象序列化的本地覆盖

[XmlType(TypeName = "UseThisElementNameInsteadOfClassName")]
public class WorkflowModule

This attribute is only honored when its the direct object being serialized

只有在序列化直接对象时才会使用此属性

[XmlRoot("UseThisElementNameWhenItIsTheRoot")]
public class WorkflowModule