13 if (attributeValue !=
null) {
14 return attributeValue;
16 throw new Exception($
"Required XML attribute \"{attributeName}\" not found in node \"{node.Name}\".");
19 public static object GetAttributeValue(XElement node,
string attributeName, Type type,
object defaultValue) {
20 XAttribute xAttribute = node.Attribute(attributeName);
21 if (xAttribute !=
null) {
39 XAttribute xAttribute = node.Attribute(attributeName);
40 if (xAttribute !=
null) {
41 xAttribute.Value = value2;
44 node.Add(
new XAttribute(attributeName, value2));
48 public static XElement
FindChildElement(XElement node,
string elementName,
bool throwIfNotFound) {
49 XElement xElement = node.Elements(elementName).FirstOrDefault();
50 if (xElement !=
null) {
53 if (throwIfNotFound) {
54 throw new Exception($
"Required XML element \"{elementName}\" not found in node \"{node.Name}\".");
59 public static XElement
AddElement(XElement parentNode,
string name) {
60 XElement xElement =
new(name);
61 parentNode.Add(xElement);
66 XmlReaderSettings xmlReaderSettings =
new() { CheckCharacters =
false, IgnoreComments =
true, IgnoreProcessingInstructions =
true };
67 using (XmlReader reader = XmlReader.Create(textReader, xmlReaderSettings)) {
68 return XElement.Load(reader, LoadOptions.None);
72 public static XElement
LoadXmlFromStream(Stream stream, Encoding encoding,
bool throwOnError) {
73 using (StreamReader textReader = encoding !=
null ?
new StreamReader(stream, encoding) :
new StreamReader(stream,
true)) {
85 XmlWriterSettings xmlWriterSettings =
new() { OmitXmlDeclaration =
true, Indent =
true, Encoding = Encoding.UTF8, CloseOutput =
true };
86 using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, xmlWriterSettings)) {
92 public static void SaveXmlToStream(XElement node, Stream stream, Encoding encoding,
bool throwOnError) {
93 using (TextWriter textWriter = encoding !=
null ?
new StreamWriter(stream, encoding) :
new StreamWriter(stream)) {
99 using (StringWriter stringWriter =
new()) {
101 return stringWriter.ToString();
Game.IContentReader.StringReader StringReader
static string ConvertToString(object value)
static object ConvertFromString(Type type, string data)
static T GetAttributeValue< T >(XElement node, string attributeName)
static void SetAttributeValue(XElement node, string attributeName, object value)
static object GetAttributeValue(XElement node, string attributeName, Type type, object defaultValue)
static object GetAttributeValue(XElement node, string attributeName, Type type)
static XElement LoadXmlFromTextReader(TextReader textReader, bool throwOnError)
static void SaveXmlToTextWriter(XElement node, TextWriter textWriter, bool throwOnError)
static XElement LoadXmlFromStream(Stream stream, Encoding encoding, bool throwOnError)
static XElement LoadXmlFromString(string data, bool throwOnError)
static XElement AddElement(XElement parentNode, string name)
static XElement FindChildElement(XElement node, string elementName, bool throwIfNotFound)
static void SaveXmlToStream(XElement node, Stream stream, Encoding encoding, bool throwOnError)
static string SaveXmlToString(XElement node, bool throwOnError)