RNode class
public sealed class RNodeA node of a tree of data, read from JSON or XML and written back.
Constructors
RNode(RNodeKind kind, string name, string value = null, bool isText = true)A node of the given kind; value is the text of a value, attribute or element.
Properties
RNodeKind Kind { get; }What the node is.
string Name { get; set; }The name: the key of an object member, «[i]» in an array, «@name» for an attribute.
string Value { get; }The value as text (null for objects, arrays, JSON null, elements without text).
bool IsText { get; }Whether a JSON value is a text, written with the quotes (false: number, true, false).
RNode Parent { get; }The node above (null for the root).
object Source { get; set; }What the node was read from: the JsonNode, the XElement or the XAttribute (null if built).
object Tag { get; set; }Anything the caller wants to keep with the node.
IReadOnlyList<RNode> Children { get; }The children, in file order.
int Count { get; }How many children.
RNode this[string name] { get; }The child with the name (null if none): node["server"], node["@port"].
Methods
static RNode Object(string name = "")A JSON object.
static RNode Array(string name = "")A JSON array.
static RNode Text(string name, string value)A JSON text value.
static RNode Number(string name, double value)A JSON number, with all its digits.
static RNode Bool(string name, bool value)A JSON true or false.
static RNode Element(string name, string text = null)An XML element, with its text if given.
static RNode Attribute(string name, string value)An XML attribute (the name without «@»).
RNode At(int index)The child at the position (null if out of range).
RNode Child(string name)The child with the name (null if none).
RNode Add(params RNode[] children)Adds children at the end. In an array they are renamed «[i]» by their position; a child taken from another node leaves it.
bool Remove(RNode child)Removes a child; false if it is not a child of this node.
RNode Set(string text)Changes a value to a text (quoted in JSON).
RNode Set(double number)Changes a value to a number, with all its digits.
RNode Set(bool flag)Changes a value to true or false.
string Path(string separator = ".")The path from the root: the names joined by the separator, the names «[i]» attached without it, the root left out when it has no name (the root of a JSON document). «server.port», «parts[2].value».
RNode Find(string path, string separator = ".")The node at a path below this one (null if there is none): Find("server.port"), Find("parts[2].value"), Find("server/@port", "/").
IEnumerable<RNode> Descendants()The node and all the nodes below it, depth first.
string GetString(string path, string def, string separator = ".")The text at the path (the default if missing or without a value).
int GetInt(string path, int def, string separator = ".")The integer at the path (the default if missing or not an integer).
double GetDouble(string path, double def, string separator = ".")The real number at the path, written with the point (the default otherwise).
bool GetBool(string path, bool def, string separator = ".")The yes/no at the path: true, yes, on, 1 and false, no, off, 0 (the default otherwise).
List<string> GetList(string path, string separator = ".")The values of the children at the path (empty if missing): the items of an array.
string ToString()The name and the value, as a line.
static RNode Load(string file)A file read whole: XML for «.xml», JSON for every other extension. Throws JsonException or XmlException if the text is not valid.
static RNode FromJson(string json)The tree of a JSON text, with a root without a name. Comments and commas before a bracket are accepted. Throws JsonException.
static RNode FromJson(JsonNode json, string name)The tree of a JSON node already read.
static RNode FromXml(string xml)The tree of an XML text: elements with the attributes «@name» as the first children and their text as the value. Throws XmlException.
static RNode FromXml(XElement e)The tree of an element already read.
void Save(string file)Writes the file: XML if the node is an element, JSON otherwise (a temporary file, then renamed).
string ToJson()The JSON text of the node, indented, with the accents as they are.
JsonNode ToJsonNode()The node as a JsonNode. An XML element becomes an object: the attributes as «@name» members, the text as «#text», the elements by name (an array when the name repeats).
XElement ToXml()The node as an XElement. A JSON object or array becomes an element named as the node («root» for the root), its members elements, the items of an array elements «item».