2using System.Collections.Generic;
28 public void Encode(
object Object,
int? Indent, StringBuilder Json)
30 XmlElement Xml = (XmlElement)Object;
40 public static void Encode(XmlElement Xml,
int? Indent, StringBuilder Json)
49 AppendProperty(
"__name", Xml.LocalName, Indent, ref First, Json);
50 AppendProperty(
"__ns", Xml.NamespaceURI, Indent, ref First, Json);
52 foreach (XmlAttribute Attr
in Xml.Attributes)
53 AppendProperty(Attr.Name, Attr.Value, Indent, ref First, Json);
55 if (Xml.HasChildNodes)
57 Dictionary<string, LinkedList<XmlElement>> ChildElements =
null;
58 StringBuilder InnerText =
null;
60 foreach (XmlNode N
in Xml.ChildNodes)
62 if (N is XmlElement E)
64 if (ChildElements is
null)
65 ChildElements =
new Dictionary<string, LinkedList<XmlElement>>();
67 if (!ChildElements.TryGetValue(E.LocalName, out LinkedList<XmlElement> Elements))
69 Elements =
new LinkedList<XmlElement>();
70 ChildElements[E.LocalName] = Elements;
75 else if (N is XmlText ||
76 N is XmlCDataSection ||
78 N is XmlSignificantWhitespace)
80 if (InnerText is
null)
81 InnerText =
new StringBuilder();
83 InnerText.Append(N.InnerText);
87 if (!(ChildElements is
null))
89 foreach (KeyValuePair<
string, LinkedList<XmlElement>> P
in ChildElements)
91 if (P.Value.First.Next is
null)
92 AppendProperty(P.Key, P.Value.First.Value, Indent, ref First, Json);
94 AppendProperty(P.Key, P.Value, Indent, ref First, Json);
97 if (!(InnerText is
null))
99 AppendProperty(ChildElements.ContainsKey(
"value") ?
"__value" :
"value",
100 InnerText.ToString(), Indent, ref First, Json);
103 else if (!(InnerText is
null))
104 AppendProperty(
"value", InnerText.ToString(), Indent, ref First, Json);
114 Json.Append(
new string(
'\t', Indent.Value));
121 private static void AppendProperty(
string Name,
object Value,
int? Indent,
122 ref
bool First, StringBuilder Json)
132 Json.Append(
new string(
'\t', Indent.Value));
152 return ObjectType == typeof(XmlElement) ? Grade.Ok :
Grade.NotAtAll;
Helps with common JSON-related tasks.
static string Encode(string s)
Encodes a string for inclusion in JSON.
Encodes an XML Element as a JSON object.
static void Encode(XmlElement Xml, int? Indent, StringBuilder Json)
Encodes an XmlElement to JSON.
void Encode(object Object, int? Indent, StringBuilder Json)
Encodes the Object to JSON.
XmlElementJsonEncoder()
Encodes an XML Element as a JSON object.
Grade Supports(Type ObjectType)
How well the JSON encoder encodes objects of type ObjectType .
Interface for encoding objects of certain types to JSON.