1

私は非常に奇妙な問題を抱えています。オブジェクトを xml 経由でシリアライズしたいと考えています。わかりましたので、XmlSerializer の作成を開始しましたが、エラーはありませんでした。しかし、逆シリアル化すると...プロパティが設定されていません。もう1つの奇妙なことは、xmlが問題ないように見えることです。

これは私のコードの一部で、残りは単なるメソッドです...

[XmlInclude(typeof(TestMapper))]
[XmlInclude(typeof(TestSource))]
[XmlInclude(typeof(TestTarget))]
public abstract class ElementBase : IElement
{
    [XmlIgnore]
    public IElement this[int index]
    {
        get
        {
            return Childs.ElementAt(index).Value;
        }
    }

    List<ElementBase> _childSerializable;
    [XmlArrayItem(typeof(ElementBase))]
    public List<ElementBase> ChildSerializable
    {
        get
        {
            _childSerializable = Childs.Select(x => x.Value).ToList();
            return _childSerializable;
        }
        set
        {
            _childSerializable = value;
            foreach (ElementBase element in _childSerializable)
                Childs.Add(element.Index, element);
        }
    }

    protected Dictionary<object, ElementBase> _childs;
    [XmlIgnore]
    public Dictionary<object, ElementBase> Childs
    {
        get
        {
            return _childs ?? (_childs = new Dictionary<object ,ElementBase>());
        }
        set
        {
            _childs = value;
        }
    }

    object _index = -1;
    public object Index
    {
        get
        {
            return _index;
        }
        set
        {
            _index = value;
        }
    }

そして、これは私がシリアル化する方法です:

TestTarget target = new TestTarget();
TestSource source = new TestSource() { Value = "Hallo", Index = 1};
TestSource source1 = new TestSource() { Value = " Welt", Index = 2};
TestMapper mapper = new TestMapper();
target.ConnectChild(mapper);
mapper.ConnectChild(source);
mapper.ConnectChild(source1);
target.Execute();

System.IO.MemoryStream memStream = new System.IO.MemoryStream();
XmlSerializer serializer = new XmlSerializer(target.GetType());
serializer.Serialize(memStream, target);

memStream.Position = 0;

object obj = serializer.Deserialize(memStream);
target = obj as TestTarget;

memStream.Position = 0;
Console.ReadKey();

さて、これが最終結果です。

<?xml version="1.0"?>
<TestTarget xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ChildSerializable>
    <ElementBase xsi:type="TestMapper">
      <ChildSerializable>
        <ElementBase xsi:type="TestSource">
          <ChildSerializable />
          <Index xsi:type="xsd:int">1</Index>
          <Value xsi:type="xsd:string">Hallo</Value>
        </ElementBase>
        <ElementBase xsi:type="TestSource">
          <ChildSerializable />
          <Index xsi:type="xsd:int">2</Index>
          <Value xsi:type="xsd:string"> Welt</Value>
        </ElementBase>
      </ChildSerializable>
      <Index xsi:type="xsd:int">-1</Index>
    </ElementBase>
  </ChildSerializable>
  <Index xsi:type="xsd:int">-1</Index>
</TestTarget>

何が間違っていたのか、誰にもわかりません。私はそれに数時間取り組んでいますが、何も見つかりません:((

4

1 に答える 1

0

を使用していることに関係しているようですList<ElementBase>。のような単純な配列を使用するElementBase[]と、アイテムは逆シリアル化されます。

ここにいくつかの説明がありますが、私が満足できるものは何も見つかりませんでした。

于 2012-08-01T02:21:43.897 に答える