Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
XmlUtils.cs
浏览该文件的文档.
1using System;
2using System.IO;
3using System.Linq;
4using System.Text;
5using System.Xml;
6using System.Xml.Linq;
8
9namespace XmlUtilities {
10 public static class XmlUtils {
11 public static object GetAttributeValue(XElement node, string attributeName, Type type) {
12 object attributeValue = GetAttributeValue(node, attributeName, type, null);
13 if (attributeValue != null) {
14 return attributeValue;
15 }
16 throw new Exception($"Required XML attribute \"{attributeName}\" not found in node \"{node.Name}\".");
17 }
18
19 public static object GetAttributeValue(XElement node, string attributeName, Type type, object defaultValue) {
20 XAttribute xAttribute = node.Attribute(attributeName);
21 if (xAttribute != null) {
22 try {
23 return HumanReadableConverter.ConvertFromString(type, xAttribute.Value);
24 }
25 catch (Exception) {
26 return defaultValue;
27 }
28 }
29 return defaultValue;
30 }
31
32 public static T GetAttributeValue<T>(XElement node, string attributeName) => (T)GetAttributeValue(node, attributeName, typeof(T));
33
34 public static T GetAttributeValue<T>(XElement node, string attributeName, T defaultValue) =>
35 (T)GetAttributeValue(node, attributeName, typeof(T), defaultValue);
36
37 public static void SetAttributeValue(XElement node, string attributeName, object value) {
38 string value2 = HumanReadableConverter.ConvertToString(value);
39 XAttribute xAttribute = node.Attribute(attributeName);
40 if (xAttribute != null) {
41 xAttribute.Value = value2;
42 }
43 else {
44 node.Add(new XAttribute(attributeName, value2));
45 }
46 }
47
48 public static XElement FindChildElement(XElement node, string elementName, bool throwIfNotFound) {
49 XElement xElement = node.Elements(elementName).FirstOrDefault();
50 if (xElement != null) {
51 return xElement;
52 }
53 if (throwIfNotFound) {
54 throw new Exception($"Required XML element \"{elementName}\" not found in node \"{node.Name}\".");
55 }
56 return null;
57 }
58
59 public static XElement AddElement(XElement parentNode, string name) {
60 XElement xElement = new(name);
61 parentNode.Add(xElement);
62 return xElement;
63 }
64
65 public static XElement LoadXmlFromTextReader(TextReader textReader, bool throwOnError) {
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);
69 }
70 }
71
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)) {
74 return LoadXmlFromTextReader(textReader, throwOnError);
75 }
76 }
77
78 public static XElement LoadXmlFromString(string data, bool throwOnError) {
79 using (StringReader textReader = new(data)) {
80 return LoadXmlFromTextReader(textReader, throwOnError);
81 }
82 }
83
84 public static void SaveXmlToTextWriter(XElement node, TextWriter textWriter, bool throwOnError) {
85 XmlWriterSettings xmlWriterSettings = new() { OmitXmlDeclaration = true, Indent = true, Encoding = Encoding.UTF8, CloseOutput = true };
86 using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, xmlWriterSettings)) {
87 node.Save(xmlWriter);
88 xmlWriter.Flush();
89 }
90 }
91
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)) {
94 SaveXmlToTextWriter(node, textWriter, throwOnError);
95 }
96 }
97
98 public static string SaveXmlToString(XElement node, bool throwOnError) {
99 using (StringWriter stringWriter = new()) {
100 SaveXmlToTextWriter(node, stringWriter, throwOnError);
101 return stringWriter.ToString();
102 }
103 }
104 }
105}
Game.IContentReader.StringReader StringReader
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)