1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
 
namespace Top.Api.Util
{
    public class XmlWriter
    {
        private StringBuilder buf = new StringBuilder();
        private Stack<object> calls = new Stack<object>();
        private string rootTagName;
        private Type stopType;
        private IDictionary<string, object> stopProps;
 
        public XmlWriter(string rootTagName, Type stopType)
        {
            this.rootTagName = rootTagName;
            this.stopType = stopType;
            if (stopType != null)
            {
                this.stopProps = GetStopProps(stopType);
            }
        }
 
        public XmlWriter()
            : this(null, null)
        {
        }
 
        public string Write(object obj)
        {
            buf.Length = 0;
            string tagName = rootTagName;
            if (tagName == null)
            {
                tagName = StringUtil.ToCamelStyle(obj.GetType().Name);
            }
            AddPair(tagName, obj);
            return buf.ToString();
        }
 
        private void Value(object obj)
        {
            if (obj == null || Cyclic(obj))
            {
                AddObject(null);
            }
            else
            {
                calls.Push(obj);
                Type objType = obj.GetType();
                if (typeof(IDictionary).IsAssignableFrom(objType))
                {
                    AddDictionary(obj as IDictionary);
                }
                else if (typeof(ICollection).IsAssignableFrom(objType))
                {
                    AddCollection(obj as ICollection);
                }
                else if (objType.IsArray)
                {
                    AddArray(obj);
                }
                else if (typeof(DateTime) == objType)
                {
                    AddDateTime((DateTime)obj);
                }
                else if (typeof(bool) == objType)
                {
                    AddBool((bool)obj);
                }
                else if (typeof(string) == objType || typeof(Type) == objType)
                {
                    AddString(obj);
                }
                else if (objType.IsPrimitive)
                {
                    AddString(obj);
                }
                else
                {
                    AddBean(obj);
                }
                calls.Pop();
            }
        }
 
        private bool Cyclic(object obj)
        {
            Stack<object>.Enumerator em = calls.GetEnumerator();
            while (em.MoveNext())
            {
                object called = em.Current;
                if (obj == called) return true;
            }
            return false;
        }
 
        private void AddBean(object obj)
        {
            bool isChildren = stopType != null && stopType.IsAssignableFrom(obj.GetType());
            PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
            foreach (PropertyInfo prop in props)
            {
                string name = prop.Name;
                if (isChildren && stopProps.ContainsKey(name))
                {
                    continue;
                }
                object value = prop.GetValue(obj, new object[] { });
                if (value != null)
                {
                    string newName = StringUtil.ToCamelStyle(name);
                    AddPair(newName, value);
                }
            }
        }
 
        private void AddDictionary(IDictionary obj)
        {
            foreach (DictionaryEntry entry in obj)
            {
                string strKey = entry.Key.ToString();
                AddPair(strKey, entry.Value);
            }
        }
 
        private void AddCollection(ICollection obj)
        {
            string tagName = null;
            foreach (object item in obj)
            {
                if (tagName == null)
                {
                    object[] apiListTypes = item.GetType().GetCustomAttributes(typeof(ApiListTypeAttribute), false);
                    if (apiListTypes != null && apiListTypes.Length > 0)
                    {
                        tagName = ((ApiListTypeAttribute) apiListTypes[0]).Value;
                    }
                    else
                    {
                        tagName = StringUtil.ToCamelStyle(obj.GetType().Name);
                    }
                }
                AddPair(tagName, item);
            }
        }
 
        private void AddArray(object obj)
        {
            string tagName = null;
            Array.ForEach(obj as object[], item =>
            {
                if (tagName == null)
                {
                    tagName = StringUtil.ToCamelStyle(obj.GetType().Name);
                }
                AddPair(tagName, item);
            });
        }
 
        private void AddBool(bool value)
        {
            AddObject(value ? "true" : "false");
        }
 
        private void AddDateTime(DateTime value)
        {
            AddObject(StringUtil.FormatDateTime(value));
        }
 
        private void AddString(object obj)
        {
            AddObject(StringUtil.EscapeXml(obj.ToString()));
        }
 
        private void AddObject(object obj)
        {
            buf.Append(obj);
        }
 
        private void AddPair(string name, object value)
        {
            StartTag(name);
            Value(value);
            EndTag(name);
        }
 
        private void StartTag(string tagName)
        {
            buf.Append("<");
            buf.Append(tagName);
            buf.Append(">");
        }
 
        private void EndTag(string tagName)
        {
            buf.Append("</");
            buf.Append(tagName);
            buf.Append(">");
        }
 
        private IDictionary<string, object> GetStopProps(Type type)
        {
            IDictionary<string, object> stopProps = new Dictionary<string, object>();
            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
            foreach (PropertyInfo prop in props)
            {
                stopProps.Add(prop.Name, null);
            }
            return stopProps;
        }
    }
}