5

我使用 XML 序列化来读取我的 Config-POCO。

为了在 Visual Studio 中获得对 XML 文件的智能支持,我需要一个模式文件。我可以使用 xsd.exe mylibrary.dll 创建架构,并且效果很好。

但是如果我将对象序列化到文件系统,我希望始终创建模式。有什么办法不使用 xsd.exe?

4

4 回答 4

12

谢谢,这对我来说是正确的方法。解决方案:

XmlReflectionImporter importer = new XmlReflectionImporter();
XmlSchemas schemas = new XmlSchemas();
XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);
Type type = toSerialize.GetType();
XmlTypeMapping map = importer.ImportTypeMapping(type);
exporter.ExportTypeMapping(map);

TextWriter tw = new StreamWriter(fileName + ".xsd");
schemas[0].Write(tw);
tw.Close();
于 2008-12-03T17:56:44.607 回答
11

Will 上面发布的解决方案效果很好,除了我意识到生成的模式没有反映不同类成员的属性。例如,用序列化提示属性装饰的类(请参见下面的示例)将无法正确呈现。

    public class Test
    {
        [XmlAttribute()]
        public string Attribute { get; set; }
        public string Description { get; set; }

        [XmlArray(ElementName = "Customers")]
        [XmlArrayItem(ElementName = "Customer")]
        public List<CustomerClass> blah { get; set; }

    }

为了解决这个问题,我创建了一些帮助函数,它们使用反射来遍历类层次结构、读取属性并填充可以传递到 XmlReflectionImporter 的 XmlAttributeOverrides 对象。

    public static void AttachXmlAttributes(XmlAttributeOverrides xao, Type t)
    {
        List<Type> types = new List<Type>();
        AttachXmlAttributes(xao, types, t);
    }

    public static void AttachXmlAttributes(XmlAttributeOverrides xao, List<Type> all, Type t)
    {
        if(all.Contains(t))
            return;
        else
            all.Add(t);

        XmlAttributes list1 = GetAttributeList(t.GetCustomAttributes(false));
        xao.Add(t, list1);

        foreach (var prop in t.GetProperties())
        {
            XmlAttributes list2 = GetAttributeList(prop.GetCustomAttributes(false));
            xao.Add(t, prop.Name, list2);
            AttachXmlAttributes(xao, all, prop.PropertyType);
        }
    }

    private static XmlAttributes GetAttributeList(object[] attributes)
    {
        XmlAttributes list = new XmlAttributes();
        foreach (var attribute in attributes)
        {
            Type type = attribute.GetType();
            if (type.Name == "XmlAttributeAttribute") list.XmlAttribute = (XmlAttributeAttribute)attribute;
            else if (type.Name == "XmlArrayAttribute") list.XmlArray = (XmlArrayAttribute)attribute;
            else if (type.Name == "XmlArrayItemAttribute") list.XmlArrayItems.Add((XmlArrayItemAttribute)attribute);

        }
        return list;
    }
    public static string GetSchema<T>()
    {
        XmlAttributeOverrides xao = new XmlAttributeOverrides();
        AttachXmlAttributes(xao, typeof(T));

        XmlReflectionImporter importer = new XmlReflectionImporter(xao);
        XmlSchemas schemas = new XmlSchemas();
        XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);
        XmlTypeMapping map = importer.ImportTypeMapping(typeof(T));
        exporter.ExportTypeMapping(map);

        using (MemoryStream ms = new MemoryStream())
        {
            schemas[0].Write(ms);
            ms.Position = 0;
            return new StreamReader(ms).ReadToEnd();
        }
    }

希望这对其他人有帮助。

于 2009-10-23T17:58:53.220 回答
3

看看System.Xml.Serialization.XmlSchemaExporter班级。我不记得确切的细节,但该命名空间中有足够的功能可以满足您的需求。

于 2008-12-03T12:57:51.870 回答
0

对 Matt Murrell 版本的改进:为嵌套属性用户类型(例如 CustomerClass 属性)递归应用 XmlAttributes。

private static void AttachXmlAttributes(XmlAttributeOverrides xao, List<Type> all, Type t)
{
    if (all.Contains(t))
    {
        return;
    }
    else
    {
        all.Add(t);
    }

    var list1 = GetAttributeList(t.GetCustomAttributes(false));
    xao.Add(t, list1);

    foreach (var prop in t.GetProperties())
    {
        var propType = prop.PropertyType;
        if (propType.IsGenericType) // is list?
        {
            var args = propType.GetGenericArguments();
            if (args != null && args.Length == 1)
            {                        
                var genType = args[0];
                if (genType.Name.ToLower() != "object")
                {
                    var list2 = GetAttributeList(prop.GetCustomAttributes(false));
                    xao.Add(t, prop.Name, list2);
                    AttachXmlAttributes(xao, all, genType);
                }                        
            }
        }
        else
        {
            var list2 = GetAttributeList(prop.GetCustomAttributes(false));
            xao.Add(t, prop.Name, list2);
            AttachXmlAttributes(xao, all, prop.PropertyType);
        }
    }
}        

private static XmlAttributes GetAttributeList(object[] attributes)
{
    var list = new XmlAttributes();
    foreach (var attr in attributes)
    {
        Type type = attr.GetType();
        switch (type.Name)
        {
            case "XmlAttributeAttribute":
                list.XmlAttribute = (XmlAttributeAttribute)attr;
                break;                    
            case "XmlRootAttribute":
                list.XmlRoot = (XmlRootAttribute)attr;
                break;
            case "XmlElementAttribute":
                list.XmlElements.Add((XmlElementAttribute)attr);
                break;
            case "XmlArrayAttribute":
                list.XmlArray = (XmlArrayAttribute)attr;
                break;
            case "XmlArrayItemAttribute":
                list.XmlArrayItems.Add((XmlArrayItemAttribute)attr);
                break;
        }
    }
    return list;
}
于 2017-08-08T12:23:14.793 回答