简体   繁体   English

反序列化不同类型的XML字符串

[英]De-serialize XML string of different types

I want to deserialize my XML string with different types, the method will have string as input parameter and it will accept 5 types of XML string. 我想反序列化具有不同类型的XML字符串,该方法将字符串作为输入参数,并且将接受5种类型的XML字符串。

Currently my plan is to choose the class to deserialize the XML String based on the string.StartsWith method to find the type. 目前,我的计划是选择基于string.StartsWith方法反序列化XML String的类以查找类型。

Is there any better way to find the type of class for deserialization? 有没有更好的方法来找到反序列化的类类型?

My first recommendation would be to allow the user to select the type when they enter the input so that you don't have to read the string to find the type. 我的第一个建议是允许用户在输入输入时选择类型,这样您就不必阅读字符串即可找到类型。

However, if you really, really wanted to, you could do something like this. 但是,如果您确实想要这样做,则可以执行以下操作。 For the purpose of testing I serialized a class called MyClass . 为了进行测试,我序列化了一个名为MyClass的类。 I'm all creative. 我都很有创造力。

var input = @"<MyClass xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""><MyString>Scott</MyString></MyClass>";

var xml = new XmlDocument();
xml.LoadXml(input);
//xml could be null
//Get the tag name of the root element which is
//the name of the serialized type.
var typeName = xml.DocumentElement.Name;
//Try to create a type from that root element's name.
var type = Type.GetType(typeName);
//If it doesn't match a type then it will be null.
if (type != null)
{
    //create an XmlSerializer for that type and deserialize the string.
    var serializer = new XmlSerializer(type);
    using (var sr = new StringReader(input))
    {
        var deserialized = serializer.Deserialize(sr);
    }
}

That will do what you've specified. 这将完成您指定的操作。 But now just as you started with an input string which could be serialization of a few possible classes, now you've got an object ( deserialized ) which is also untyped, so I'm not quite sure what you'd do with it next. 但是现在,就像从一个可以对几个可能的类进行序列化的输入字符串开始一样,现在您有了一个也没有类型化的objectdeserialized ),因此我不确定下一步如何处理它。

If you are asking if there is an elegant way to infer the c# type based on the string, then my answer is 'not really'. 如果您问是否有一种优雅的方法可以基于字符串来推断c#类型,那么我的回答是“不是真的”。 You will have to use your favorite mechanism to inspect the input string to infer the type from the string contents. 您将必须使用自己喜欢的机制来检查输入字符串,以便从字符串内容中推断出类型。

Or simply not care about what 'type' the input is and use xpath on an xmldocument to do your work. 或者根本不关心输入是什么“类型”,而是在xmldocument上使用xpath来完成您的工作。

My favorite c# class type detection mechanism would look something like this: 我最喜欢的c#类类型检测机制将如下所示:

internal class Converter
{
    public T CovertXML<T>(string XMLString) where T:class
    {
        if (XMLString.ToLower().Contains(typeof(Type1).GetType().Name.ToLower()))
        {
            return XMLString.Deseralize<Type1>() as T;
        }
        //..etc etc.
        return null;
    }
}
internal class Type1
{
}
public static class StringExtensionMethods
{
     public static T Deseralize<T>(this string Instance)
    {
        System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
        using (System.IO.StringReader sr = new System.IO.StringReader(Instance))
            return (T)ser.Deserialize(sr);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM