Пример #1
0
    /// <summary>
    /// 循环写入xml元素
    /// </summary>
    /// <param name="xmldoc"></param>
    /// <param name="root"></param>
    /// <param name="name"></param>
    private static void WriteToXmlElement(XmlDocument xmldoc, XmlElement root, string name)
    {
        try
        {
            Type type = GetType(name);
            if (type != null)
            {
                System.Object obj = null;
                if (type == typeof(System.String))
                {
                    obj = "";
                }
                else
                {
                    obj = Activator.CreateInstance(type);
                }
                if (obj is BaseData)
                {
                    BaseData       data  = (BaseData)obj;
                    PropertyInfo[] infos = data.AllAttributes();

                    foreach (PropertyInfo info in infos)
                    {
                        string propertyStr = info.PropertyType.ToString();
                        string listStr     = typeof(List <>).ToString();

                        if (propertyStr.Contains("List") && !IsStandardList(propertyStr))
                        {
                            if (propertyStr.Substring(0, propertyStr.LastIndexOf('[')) == listStr.Substring(0, listStr.LastIndexOf('[')))
                            {
                                XmlElement tempElement = xmldoc.CreateElement("variable");
                                tempElement.SetAttribute("name", info.Name);
                                tempElement.SetAttribute("type", StandardType(info.PropertyType));
                                //tempElement.SetAttribute("defultvalue", "");
                                //tempElement.SetAttribute("foreign", "");
                                //tempElement.SetAttribute("split", "");

                                string     typeName = propertyStr.Substring(propertyStr.LastIndexOf('[') + 1).Replace("]", "");
                                XmlElement ele      = xmldoc.CreateElement("list");
                                ele.SetAttribute("name", typeName);
                                ele.SetAttribute("sheetname", typeName);
                                ele.SetAttribute("mainkey", "Id");
                                WriteToXmlElement(xmldoc, ele, typeName);
                                tempElement.AppendChild(ele);
                                root.AppendChild(tempElement);
                            }
                        }
                        else
                        {
                            XmlElement ele = xmldoc.CreateElement("variable");
                            ele.SetAttribute("name", info.Name);
                            ele.SetAttribute("col", info.Name);
                            ele.SetAttribute("type", StandardType(info.PropertyType));
                            //ele.SetAttribute("defultvalue", "");
                            //ele.SetAttribute("foreign", "");
                            if (IsStandardList(propertyStr))
                            {
                                ele.SetAttribute("split", ";");
                                string str = "list" + propertyStr.Substring(propertyStr.LastIndexOf('['));
                                ele.SetAttribute("type", str);
                            }
                            root.AppendChild(ele);
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }