c# – 使用不同的根元素名称反序列化

下面的例子告诉我“[One xmlns =”]不是预期的.”例外

public abstract class BaseClass{ }

[XmlRoot("One")]
public class ChildOne : BaseClass {}

[XmlRoot("Two")]
public class ChildTwo : BaseClass { }

class Program
{
    private static void Main(string[] args)
    {
        var ser = new XmlSerializer(typeof (BaseClass), new Type[] {typeof (ChildOne), typeof (ChildTwo)});
        var obj1 = ser.Deserialize(new StringReader(@"<?xml version=""1.0""?><One></One>"));
        var obj2 = ser.Deserialize(new StringReader(@"<?xml version=""1.0""?><Two></Two>"));
    }
}

我需要反序列化XML(不是由我生成的).根标签可能有不同的名称,我必须映射到不同的类.

PS.我知道有很多这样的问题.我研究过它们但我的问题仍未解决.

解决方法:

我的问题没有大肆宣传.社区可能会认为这只是一个白痴问的另一个愚蠢的问题.他们可能是对的.我必须自己回答,但要注意:答案也可能是愚蠢的.

我最终探测了XML的根元素,然后将其映射到一种已知类型并使用该类型进行反序列化.像这样:

public abstract class BaseClass{ }

[XmlRoot("One")]
public class ChildOne : BaseClass {}

[XmlRoot("Two")]
public class ChildTwo : BaseClass { }

class Program
{
    private static void Main(string[] args)
    {
        var known = new Type[] {typeof (ChildOne), typeof (ChildTwo)};
        var obj1 = Deserialize<BaseClass>(@"<?xml version=""1.0""?><One></One>", known);
        var obj2 = Deserialize<BaseClass>(@"<?xml version=""1.0""?><Two></Two>", known);
    }

    private static T Deserialize<T>(string xml, Type[] knownTypes)
    {
        Type rootType = typeof (T);

        if (knownTypes.Any())
        {
            using (var reader = XmlReader.Create(new StringReader(xml)))
            {
                reader.MoveToContent();

                rootType = (from kt in knownTypes
                        let xmlRoot = kt.GetCustomAttributes<XmlRootAttribute>().FirstOrDefault()
                        where kt.Name == reader.Name || (xmlRoot != null && xmlRoot.ElementName == reader.Name)
                        select kt).FirstOrDefault() ?? typeof(T);
            }
        }

        return (T) new XmlSerializer(rootType, knownTypes).Deserialize(new StringReader(xml));
    }
}
上一篇:JS BOM篇(一)window对象


下一篇:dom的宽高 clientWidth offsetWidth scrollWidth