1

我希望能从你那里找到解决办法。我需要将 ValidatorList 类对象序列化为 xml 文档。这个怎么做?

我试过这样:

XmlSerializer _serializer = new XmlSerializer(list); 

但我不知道如何为具有多个类的列表输出 xml。

C#

_list= new ListVal();
 Type _type = typeof(ras);

 _list.Add(new RequiredField
                    {
                      Property = _type.GetProperty("CustRef")
                    }.Add(new AsciiVal()));
_list.Add(new RequiredField
                  {
                    Property = _type.GetProperty("ctr")
                  }.Add(new StringLengthVal
                          {
                            min= 3,
                            max= 3
                          }));

[Serializable]
 public class Field
 {
public Field Next
{
  get;
  set;
}

public Field TypeName
{
  get;
  set;
}

    public Field PropertyName
  {
  get;
  set;
}
}

public class RequiredField : Field
{
//TODO
 }

public class AsciiVal: Field
 {
//TODO
  }

public class StringLengthVal: Field
 {
//TODO
}

 public class ListVal: List<Field>
 {
//TODO
  }
4

3 回答 3

1

我有一些接近的东西,但不完全是你想要的 Xml。实际上,我认为您会发现下面生成的 Xml 比您所拥有的更有意义。

System.Xml.Serialization为了让您开始,您可以使用命名空间中的属性来控制序列化和反序列化。一些有用的阅读是

因此,我模拟了一些与您自己的代码非常匹配的代码。请注意添加了一些属性来指示序列化程序我希望如何布置 Xml。

[XmlInclude(typeof(AsciiValidator))]
[XmlInclude(typeof(RequiredValidator))]
[XmlInclude(typeof(StringLengthValidator))]
public class FieldValidator
{
    [XmlElement("Next")]
    public FieldValidator Next
    {
    get;
    set;
    }

    [XmlElement("PropertyName")]
    public string PropertyName
    {
    get;
    set;
    }
}

public class AsciiValidator: FieldValidator
{
}

public class RequiredValidator: FieldValidator
{
}

public class StringLengthValidator: FieldValidator
{
    [XmlElement]
    public int MinLength{get;set;}
    [XmlElement]
    public int MaxLength{get;set;}
}

[XmlRoot("ValidatorList")]
public class ValidatorList : List<FieldValidator>
{    
}

兴趣点;FieldValidator必须将每个继承的类添加到已知类型列表中,XmlIncludeAttribute以便序列化程序知道如何处理它们)

然后我创建了一个示例对象映射:

var test = new ValidatorList();
test.Add(
            new RequiredValidator()
                {
                    PropertyName="CustRef",
                    Next = new AsciiValidator()
                });            
test.Add(
            new RequiredValidator()
                {
                    PropertyName="CurrencyIndicator",
                        Next = new StringLengthValidator(){
                            MinLength=3,
                            MaxLength = 10
                        }
                });

最后我告诉序列化器序列化它(并将结果输出到控制台)

var ser = new XmlSerializer(typeof(ValidatorList));
ser.Serialize(Console.Out,test);

结果是这样的:

<?xml version="1.0" encoding="utf-8"?>
<ValidatorList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FieldValidator xsi:type="RequiredValidator">
    <Next xsi:type="AsciiValidator" />
    <PropertyName>CustRef</PropertyName>
  </FieldValidator>
  <FieldValidator xsi:type="RequiredValidator">
    <Next xsi:type="StringLengthValidator">
      <MinLength>3</MinLength>
      <MaxLength>10</MaxLength>
    </Next>
    <PropertyName>CurrencyIndicator</PropertyName>
  </FieldValidator>
</ValidatorList>

离你想要的东西不到一百万英里。需要以某种方式输出某些东西(例如xsi:type告诉序列化器如何反序列化回对象映射)。我希望这能给你一个好的开始。

这是一个实时的工作示例:http ://rextester.com/OXPOB95358

可以通过调用DeserializeXmlSerializer 上的方法来完成反序列化。

例如,如果您的 xml 在字符串中:

var ser = new XmlSerializer(typeof(ValidatorList));
var test = "<..../>" // Your Xml
var xmlReader = XmlReader.Create(new StringReader(test));
var validatorList = (ValidatorList)ser.Deserialize(xmlReader);

Deserialize 有许多覆盖,它们采用不同的输入,具体取决于数据是在现有阅读器的 Stream 中,还是保存到文件中。

于 2013-07-09T10:51:40.893 回答
0

I have several SO answers that detail how to serialize objects using XML. I'll provide links below.

However, since you're looking for a rather simple serialization of your object, you may want to read up on the DataContractSerializer. It's much less complicated than the old .NET 1.x XML Serialization.

Here's the list of SO answers:

Even though many of these have to do with XML serialization and namespaces, they contain complete examples of serializing an object to XML using .NET 1.x XML Serialization.

于 2013-07-09T13:14:26.640 回答
0

您必须使用 KonwnType 属性装饰包含 _validators 字段的类

[Serializable]
[KwownType(typeof(RequiredFieldValidator)]
[KwownType(typeof(AsciValidator)]
public class MySerialisableClass
于 2013-07-09T10:49:03.513 回答