Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
StreamingElementWriter.cs
Go to the documentation of this file.
3
4namespace System.Xml.Linq;
5
6internal struct StreamingElementWriter
7{
8 private readonly XmlWriter _writer;
9
11
12 private readonly List<XAttribute> _attributes;
13
15
17 {
18 _writer = w;
19 _element = null;
22 }
23
24 private void FlushElement()
25 {
26 if (_element == null)
27 {
28 return;
29 }
32 _writer.WriteStartElement(GetPrefixOfNamespace(@namespace, allowDefaultNamespace: true), _element.Name.LocalName, @namespace.NamespaceName);
33 foreach (XAttribute attribute in _attributes)
34 {
35 @namespace = attribute.Name.Namespace;
36 string localName = attribute.Name.LocalName;
37 string namespaceName = @namespace.NamespaceName;
38 _writer.WriteAttributeString(GetPrefixOfNamespace(@namespace, allowDefaultNamespace: false), localName, (namespaceName.Length == 0 && localName == "xmlns") ? "http://www.w3.org/2000/xmlns/" : namespaceName, attribute.Value);
39 }
40 _element = null;
41 _attributes.Clear();
42 }
43
45 {
46 string namespaceName = ns.NamespaceName;
47 if (namespaceName.Length == 0)
48 {
49 return string.Empty;
50 }
52 if (prefixOfNamespace != null)
53 {
54 return prefixOfNamespace;
55 }
56 if ((object)namespaceName == "http://www.w3.org/XML/1998/namespace")
57 {
58 return "xml";
59 }
60 if ((object)namespaceName == "http://www.w3.org/2000/xmlns/")
61 {
62 return "xmlns";
63 }
64 return null;
65 }
66
67 private void PushElement()
68 {
69 _resolver.PushScope();
70 foreach (XAttribute attribute in _attributes)
71 {
72 if (attribute.IsNamespaceDeclaration)
73 {
74 _resolver.Add((attribute.Name.NamespaceName.Length == 0) ? string.Empty : attribute.Name.LocalName, XNamespace.Get(attribute.Value));
75 }
76 }
77 }
78
79 private void Write(object content)
80 {
81 if (content == null)
82 {
83 return;
84 }
85 if (content is XNode n)
86 {
87 WriteNode(n);
88 return;
89 }
90 if (content is string s)
91 {
92 WriteString(s);
93 return;
94 }
95 if (content is XAttribute a)
96 {
97 WriteAttribute(a);
98 return;
99 }
100 if (content is XStreamingElement e)
101 {
102 WriteStreamingElement(e);
103 return;
104 }
105 if (content is object[] array)
106 {
107 object[] array2 = array;
108 foreach (object content2 in array2)
109 {
110 Write(content2);
111 }
112 return;
113 }
114 if (content is IEnumerable enumerable)
115 {
116 {
117 foreach (object item in enumerable)
118 {
119 Write(item);
120 }
121 return;
122 }
123 }
124 WriteString(XContainer.GetStringValue(content));
125 }
126
128 {
129 if (_element == null)
130 {
132 }
133 _attributes.Add(a);
134 }
135
136 private void WriteNode(XNode n)
137 {
138 FlushElement();
139 n.WriteTo(_writer);
140 }
141
143 {
144 FlushElement();
145 _element = e;
146 Write(e.content);
147 FlushElement();
148 _writer.WriteEndElement();
149 _resolver.PopScope();
150 }
151
152 private void WriteString(string s)
153 {
154 FlushElement();
155 _writer.WriteString(s);
156 }
157}
void Add(TKey key, TValue value)
static string InvalidOperation_WriteAttribute
Definition SR.cs:62
Definition SR.cs:7
static string GetStringValue(object value)
static XNamespace Get(string namespaceName)
Definition XNamespace.cs:51
string GetPrefixOfNamespace(XNamespace ns, bool allowDefaultNamespace)
void Add(string prefix, XNamespace ns)
string GetPrefixOfNamespace(XNamespace ns, bool allowDefaultNamespace)