diff --git a/LibreMetaverse.StructuredData/LLSD/XmlLLSD.cs b/LibreMetaverse.StructuredData/LLSD/XmlLLSD.cs index be58cb44..7406d2cf 100644 --- a/LibreMetaverse.StructuredData/LLSD/XmlLLSD.cs +++ b/LibreMetaverse.StructuredData/LLSD/XmlLLSD.cs @@ -50,12 +50,14 @@ namespace OpenMetaverse.StructuredData /// public static OSD DeserializeLLSDXml(byte[] xmlData) { - return DeserializeLLSDXml(new XmlTextReader(new MemoryStream(xmlData, false))); + using(XmlTextReader xrd = new XmlTextReader(new MemoryStream(xmlData, false))) + return DeserializeLLSDXml(xrd); } public static OSD DeserializeLLSDXml(Stream xmlStream) { - return DeserializeLLSDXml(new XmlTextReader(xmlStream)); + using(XmlTextReader xrd = new XmlTextReader(xmlStream)) + return DeserializeLLSDXml(xrd); } /// @@ -66,7 +68,8 @@ namespace OpenMetaverse.StructuredData public static OSD DeserializeLLSDXml(string xmlData) { byte[] bytes = Utils.StringToBytes(xmlData); - return DeserializeLLSDXml(new XmlTextReader(new MemoryStream(bytes, false))); + using(XmlTextReader xrd = new XmlTextReader(new MemoryStream(bytes, false))) + return DeserializeLLSDXml(xrd); } /// @@ -110,16 +113,29 @@ namespace OpenMetaverse.StructuredData public static string SerializeLLSDXmlString(OSD data) { StringWriter sw = new StringWriter(); - XmlTextWriter writer = new XmlTextWriter(sw); - writer.Formatting = Formatting.None; + using(XmlTextWriter writer = new XmlTextWriter(sw)) + { + writer.Formatting = Formatting.None; - writer.WriteStartElement(String.Empty, "llsd", String.Empty); - SerializeLLSDXmlElement(writer, data); - writer.WriteEndElement(); + writer.WriteStartElement(String.Empty, "llsd", String.Empty); + SerializeLLSDXmlElement(writer, data); + writer.WriteEndElement(); - writer.Close(); + return sw.ToString(); + } + } - return sw.ToString(); + public static string SerializeLLSDInnerXmlString(OSD data) + { + StringWriter sw = new StringWriter(); + using (XmlTextWriter writer = new XmlTextWriter(sw)) + { + writer.Formatting = Formatting.None; + + SerializeLLSDXmlElement(writer, data); + + return sw.ToString(); + } } /// @@ -194,12 +210,17 @@ namespace OpenMetaverse.StructuredData case OSDType.Array: OSDArray array = (OSDArray)data; writer.WriteStartElement(String.Empty, "array", String.Empty); - for (int i = 0; i < array.Count; i++) + foreach (var element in array) { - SerializeLLSDXmlElement(writer, array[i]); + SerializeLLSDXmlElement(writer, element); } writer.WriteEndElement(); break; + case OSDType.LlsdXml: + writer.WriteRaw(data.AsString()); + break; + default: + break; } } @@ -223,27 +244,29 @@ namespace OpenMetaverse.StructuredData readerSettings.Schemas.Add(XmlSchema); readerSettings.ValidationEventHandler += new ValidationEventHandler(LLSDXmlSchemaValidationHandler); - XmlReader reader = XmlReader.Create(xmlData, readerSettings); + using(XmlReader reader = XmlReader.Create(xmlData, readerSettings)) + { - try - { - while (reader.Read()) { } - } - catch (XmlException) - { - error = LastXmlErrors; - return false; - } + try + { + while (reader.Read()) { } + } + catch (XmlException) + { + error = LastXmlErrors; + return false; + } - if (LastXmlErrors == String.Empty) - { - error = null; - return true; - } - else - { - error = LastXmlErrors; - return false; + if (LastXmlErrors == String.Empty) + { + error = null; + return true; + } + else + { + error = LastXmlErrors; + return false; + } } } } diff --git a/LibreMetaverse.StructuredData/StructuredData.cs b/LibreMetaverse.StructuredData/StructuredData.cs index 82d4c780..af4911c9 100644 --- a/LibreMetaverse.StructuredData/StructuredData.cs +++ b/LibreMetaverse.StructuredData/StructuredData.cs @@ -34,7 +34,7 @@ using System.Text; namespace OpenMetaverse.StructuredData { - public enum OSDType + public enum OSDType : byte { Unknown, Boolean, @@ -46,7 +46,8 @@ namespace OpenMetaverse.StructuredData URI, Binary, Map, - Array + Array, + LlsdXml } public enum OSDFormat @@ -553,6 +554,27 @@ namespace OpenMetaverse.StructuredData public override string ToString() { return AsString(); } } + /// + /// OSD LLSD-XML Element + /// + public sealed class OSDLlsdXml : OSD + { + public readonly string value; + public override OSDType Type => OSDType.LlsdXml; + + public override OSD Copy() { return new OSDLlsdXml(value); } + + public OSDLlsdXml(string value) + { + // Refuse to hold null pointers + this.value = value ?? string.Empty; + } + + public override string AsString() { return value; } + public override byte[] AsBinary() { return Encoding.UTF8.GetBytes(value); } + public override string ToString() { return AsString(); } + } + /// /// OSD String Element ///