Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 控制派生类型的序列化_C#_Xml_Serialization - Fatal编程技术网

C# 控制派生类型的序列化

C# 控制派生类型的序列化,c#,xml,serialization,C#,Xml,Serialization,给定一种类型,例如: public class FooList : List<Foo> { public string SomeMessage { get; set; } } 我想要的是: <FooList> <SomeMessage /> <Foo /> <Foo /> <Foo /> </FooList> 尝试使用[XMLElement]属性装饰某条消息,以强制XM

给定一种类型,例如:

public class FooList : List<Foo>
{
    public string SomeMessage { get; set; }
}
我想要的是:

<FooList>
    <SomeMessage />
    <Foo />
    <Foo />
    <Foo />
</FooList>

尝试使用[XMLElement]属性装饰某条消息,以强制XMLSerializer包含它。

XMLSerializer
序列化集合时,它只考虑集合的项。将忽略类中声明的任何额外属性。您必须实现
IXmlSerializable
以提供自己的序列化逻辑,或者更改类的设计

你可以这样做:

[XmlRoot("FooList")]
public class FooListContainer
{
    public string SomeMessage { get; set; }

    [XmlElement("Foo")]
    public List<Foo> Foos { get; set; }
}
[XmlRoot(“傻瓜”)]
公共类容器
{
公共字符串SomeMessage{get;set;}
[XmlElement(“Foo”)]
公共列表Foos{get;set;}
}

从集合类派生时,
xmlsalalizier
将仅序列化集合中的元素。解决此问题的一种方法是创建一个类,该类包装集合和
SomeMessage
,并将其序列化

  [XmlRoot("FooList")]
  public class CollectionWrapper
  {
     [XmlElement]
     public List<Foo> Items { get; set; }
     public string SomeMessage { get; set; }
  }
[XmlRoot(“傻瓜”)]
公共类集合包装器
{
[XmlElement]
公共列表项{get;set;}
公共字符串SomeMessage{get;set;}
}
然后你可以这样做:

     CollectionWrapper cw = new CollectionWrapper();
     cw.Items = new List<Foo>();
     cw.Items.Add(foo1);
     cw.Items.Add(foo2);
     cw.SomeMessage = "this is a test";

     using (TextWriter writer = new StreamWriter("C:\\foos.xml"))
     {
        XmlSerializer serializer = new XmlSerializer(typeof(CollectionWrapper));
        serializer.Serialize(writer, cw);
     }
CollectionWrapper cw=new CollectionWrapper();
cw.Items=新列表();
cw.项目。添加(foo1);
cw.项目。添加(foo2);
cw.SomeMessage=“这是一项测试”;
使用(TextWriter=newstreamWriter(“C:\\foos.xml”))
{
XmlSerializer serializer=新的XmlSerializer(typeof(CollectionWrapper));
serializer.Serialize(writer,cw);
}

可以进行详细讨论。

如果需要实现
xmlcoiceIdentifierAttribute
,XmlSerializer将能够正确处理您的需求

有关此主题的更多信息,请单击此处:

示例代码:

[System.Xml.Serialization.XmlElementAttribute("myType1", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("myType2", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public string[] Items 
{
    get 
    {
        return this.itemsField;
    }
    set 
    {
        this.itemsField = value;
    }
}
如果您希望通过
xsd.exe
生成可序列化类,那么xsd模式应该如下所示(注意
xs:choice
标记):



嗯…

我试过了。我还尝试了[XmlAttribute],希望它能做一些不同的事情……没有这样的运气:(可能是
     CollectionWrapper cw = new CollectionWrapper();
     cw.Items = new List<Foo>();
     cw.Items.Add(foo1);
     cw.Items.Add(foo2);
     cw.SomeMessage = "this is a test";

     using (TextWriter writer = new StreamWriter("C:\\foos.xml"))
     {
        XmlSerializer serializer = new XmlSerializer(typeof(CollectionWrapper));
        serializer.Serialize(writer, cw);
     }
[System.Xml.Serialization.XmlElementAttribute("myType1", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("myType2", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public string[] Items 
{
    get 
    {
        return this.itemsField;
    }
    set 
    {
        this.itemsField = value;
    }
}
<xs:complexType name="rowElement">
  <xs:sequence>
    <xs:choice maxOccurs="unbounded">
      <xs:element name="myType1" type="xs:string"/>
      <xs:element name="myType2" type="xs:string"/> 
    </xs:choice>
  </xs:sequence>
</xs:complexType>