/* * common method for tests of this suite */ private void runTest(IXmlDocument config, string cacheName, string serializerName) { IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory; IXmlElement originalConfig = ccf.Config; ccf.Config = config; INamedCache cache = ccf.EnsureCache(cacheName); cache.Clear(); // create a key, and value String sKey = "hello"; String sValue = "grid"; // insert the pair into the cache cache.Insert(sKey, sValue); // read back the value, custom serializer should have converted Assert.AreEqual(cache.Count, 1); Assert.AreEqual(cache[sKey], serializerName); ccf.DestroyCache(cache); ccf.Config = originalConfig; }
public void TestListenerEvents() { IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory; IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-near-cache-config.xml"); ccf.Config = config; INamedCache cache = CacheFactory.GetCache("dist-extend-direct"); cache.Clear(); ListenerWithWait listen = new ListenerWithWait(); cache.AddCacheListener(listen, "test", true); cache.Insert("test", "c"); CacheEventArgs localEvent = listen.WaitForEvent(2000); Assert.IsNotNull(localEvent); String value = (String)cache["test"]; localEvent = listen.WaitForEvent(4000); Assert.AreEqual("c", value); Assert.IsNull(localEvent); CacheFactory.Shutdown(); }
public void SetUp() { IConfigurationXml configFactory = new ConfigurationFactory(Substitute.For <IToastService>()).Create(Filename); const string sutFieldName = "LoadAndSave"; mSut = (ICanLoadAndSaveXml)configFactory.GetType().GetField(sutFieldName, BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(configFactory); if (mSut == null) { Assert.Fail($"Unable to find private instance field '{sutFieldName}'"); } mXmlDoc = Substitute.For <IXmlDocument>(); IXmlDocumentFactory xmlDocFactory = Substitute.For <IXmlDocumentFactory>(); xmlDocFactory.Create().Returns(mXmlDoc); SetSutField("mXmlDocFactory", xmlDocFactory); IFile file = Substitute.For <IFile>(); file.Exists(Filename).Returns(ci => mFileExists); SetSutField("mFile", file); mXmlWriterFactory = Substitute.For <IXmlWriterFactory>(); SetSutField("mXmlWriterFactory", mXmlWriterFactory); //Substitute fileStreamFactory to avoid disk access attempts IFileStreamFactory fileStreamFactory = Substitute.For <IFileStreamFactory>(); SetSutField("mFileStreamFactory", fileStreamFactory); }
public void TestSslClientConfiguration5() { var location = new IPEndPoint(IPAddress.Loopback, 5055); server = new SslServer(location) { ServerCertificate = SslServer.LoadCertificate( serverCert), AuthenticateClient = true }; server.Start(); TcpClient client = new TcpClient(); try { IXmlDocument xmlDoc = XmlHelper.LoadXml("./Net/Ssl/Configs/config5.xml"); IStreamProvider streamProvider = StreamProviderFactory.CreateProvider(xmlDoc); client.Connect(location); Stream stream = streamProvider.GetStream(client); string echo = SslClient.Echo(stream, "Hello World"); Assert.AreEqual(echo, "Hello World"); } finally { client.Close(); server.Stop(); } }
public void ConstructorTakingParameters() { XmlDocumentFactory.Type = typeof(HasConstructorWithParameter); IXmlDocument doc = XmlDocumentFactory.CreateInstance(); Assert.Null(doc); }
/// <summary> /// Appends all elements of enumeration into specified document. They need to implement at least <see cref="IXmlConvertible"/>. /// </summary> public static int Append(IXmlDocument doc, XmlNode node, IEnumerator items, string comment, bool printComment, object runtimeParams) { int counter = 0; // add comment if needed: if (printComment) { node.AppendChild(doc.CreateComment(comment)); } // go throu all the elements and serialize: if (items != null) { items.Reset(); while (items.MoveNext()) { XmlNode elem = ((IXmlConvertible)items.Current).ToXmlNode(doc, runtimeParams); // and if serialization was successful: if (elem != null) { node.AppendChild(elem); counter++; } } } return(counter); }
public void TestConnectionTimeout() { var initiator = new TcpInitiator { OperationalContext = new DefaultOperationalContext() }; Stream stream = GetType().Assembly.GetManifestResourceStream("Tangosol.Resources.s4hc-timeout-cache-config.xml"); IXmlDocument xmlConfig = XmlHelper.LoadXml(stream); IXmlElement initConfig = xmlConfig.FindElement("caching-schemes/remote-cache-scheme/initiator-config"); initiator.Configure(initConfig); initiator.RegisterProtocol(CacheServiceProtocol.Instance); initiator.RegisterProtocol(NamedCacheProtocol.Instance); initiator.Start(); Assert.AreEqual(initiator.IsRunning, true); IConnection conn = null; bool exceptionCaught = false; try { conn = initiator.EnsureConnection(); } catch (Tangosol.Net.Messaging.ConnectionException) { // good, should time out exceptionCaught = true; } Assert.IsNull(conn); Assert.IsTrue(exceptionCaught); initiator.Stop(); }
public void CreateCustomXmlDocument() { XmlDocumentFactory.Type = typeof(XmlDocumentMock); IXmlDocument doc = XmlDocumentFactory.CreateInstance(); Assert.Equals(typeof(XmlDocumentMock), doc.GetType()); }
private void ReadArrayElements(JsonReader reader, IXmlDocument document, string propertyName, IXmlNode currentNode, XmlNamespaceManager manager) { string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); IXmlElement nestedArrayElement = CreateElement(propertyName, document, elementPrefix, manager); currentNode.AppendChild(nestedArrayElement); int count = 0; while (reader.Read() && reader.TokenType != JsonToken.EndArray) { DeserializeValue(reader, document, manager, propertyName, nestedArrayElement); count++; } if (WriteArrayAttribute) { AddJsonArrayAttribute(nestedArrayElement, document); } if (count == 1 && WriteArrayAttribute) { IXmlElement arrayElement = nestedArrayElement.ChildNodes.CastValid <IXmlElement>().Single(n => n.LocalName == propertyName); AddJsonArrayAttribute(arrayElement, document); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable()); IXmlDocument xmlDocument = null; IXmlNode xmlNode = null; if (typeof(XmlNode).IsAssignableFrom(objectType)) { if (objectType != typeof(XmlDocument)) { throw new JsonSerializationException("XmlNodeConverter only supports deserializing XmlDocuments"); } XmlDocument document = new XmlDocument(); xmlDocument = new XmlDocumentWrapper(document); xmlNode = xmlDocument; } if (xmlDocument == null || xmlNode == null) { throw new JsonSerializationException("Unexpected type when converting XML: " + objectType); } if (reader.TokenType != JsonToken.StartObject) { throw new JsonSerializationException("XmlNodeConverter can only convert JSON that begins with an object."); } if (!string.IsNullOrEmpty(DeserializeRootElementName)) { ReadElement(reader, xmlDocument, xmlNode, DeserializeRootElementName, manager); } else { reader.Read(); DeserializeNode(reader, xmlDocument, manager, xmlNode); } return(xmlDocument.WrappedNode); }
static void UpdateSound(IXmlDocument content, IPredefinedToastNotificationInfo info) { if (info.Sound != PredefinedSound.Notification_Default) { SetSound(content, info.Sound); } }
public SiteMapping(String fileName) { _fileName = fileName; var parser = new XmlParser(); var content = File.Exists(fileName) ? File.ReadAllText(_fileName) : "<entries></entries>"; _xml = parser.Parse(content); }
public void TestTransformInitParams() { IXmlDocument root = new SimpleDocument("root"); IXmlDocument initParams = new SimpleDocument("init-params"); for (int i = 1; i < 4; i++) { IXmlElement paramEl = initParams.AddElement("init-param"); paramEl.AddElement("param-name").SetString("name" + i); paramEl.AddElement("param-value").SetInt(i); } Assert.AreEqual(initParams.ElementList.Count, 3); Assert.AreEqual(root.ElementList.Count, 0); XmlHelper.TransformInitParams(root, initParams); Assert.AreEqual(root.ElementList.Count, 3); for (int i = 1; i < 4; i++) { Assert.IsNotNull(root.GetElement("name" + i)); Assert.AreEqual(root.GetElement("name" + i).GetInt(), i); } IXmlDocument xml = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-util-transformparams.xml"); IXmlElement el = xml.FindElement("configurable-cache-factory-config/init-params"); root = new SimpleDocument("root"); XmlHelper.TransformInitParams(root, el); Assert.IsNotNull(root.GetElement("long")); Assert.IsNotNull(root.GetElement("date")); Assert.IsNotNull(root.GetElement("float")); }
static void SetSound(IXmlDocument xmldoc, PredefinedSound sound) { string soundXml = "ms-winsoundevent:" + sound.ToString().Replace("_", "."); IXmlElement soundElement; ComFunctions.CheckHRESULT(xmldoc.CreateElement("audio", out soundElement)); if (sound == PredefinedSound.NoSound) { ComFunctions.CheckHRESULT(soundElement.SetAttribute("silent", "true")); } else { ComFunctions.CheckHRESULT(soundElement.SetAttribute("src", soundXml)); ComFunctions.CheckHRESULT(soundElement.SetAttribute("loop", IsLoopingSound(sound).ToString().ToLower())); } var asNode = (IXmlNode)xmldoc; IXmlNode appendedChild; IXmlNodeList nodes; ComFunctions.CheckHRESULT(xmldoc.GetElementsByTagName("toast", out nodes)); IXmlNode toastNode; ComFunctions.CheckHRESULT(nodes.Item(0, out toastNode)); ComFunctions.CheckHRESULT(toastNode.AppendChild((IXmlNode)soundElement, out appendedChild)); }
private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager) { if (string.IsNullOrEmpty(propertyName)) { throw JsonSerializationException.Create(reader, "XmlNodeConverter cannot convert JSON with an empty property name to XML."); } Dictionary <string, string> strs = this.ReadAttributeElements(reader, manager); string prefix = MiscellaneousUtils.GetPrefix(propertyName); if (propertyName.StartsWith('@')) { string str = propertyName.Substring(1); string prefix1 = MiscellaneousUtils.GetPrefix(str); XmlNodeConverter.AddAttribute(reader, document, currentNode, str, manager, prefix1); return; } if (propertyName.StartsWith('$')) { if (propertyName == "$values") { propertyName = propertyName.Substring(1); prefix = manager.LookupPrefix("http://james.newtonking.com/projects/json"); this.CreateElement(reader, document, currentNode, propertyName, manager, prefix, strs); return; } if (propertyName == "$id" || propertyName == "$ref" || propertyName == "$type" || propertyName == "$value") { string str1 = propertyName.Substring(1); string str2 = manager.LookupPrefix("http://james.newtonking.com/projects/json"); XmlNodeConverter.AddAttribute(reader, document, currentNode, str1, manager, str2); return; } } this.CreateElement(reader, document, currentNode, propertyName, manager, prefix, strs); }
private void ReadArrayElements(JsonReader reader, IXmlDocument document, string propertyName, IXmlNode currentNode, XmlNamespaceManager manager) { string prefix = MiscellaneousUtils.GetPrefix(propertyName); IXmlElement xmlElement = this.CreateElement(propertyName, document, prefix, manager); currentNode.AppendChild(xmlElement); int num = 0; while (reader.Read() && reader.TokenType != JsonToken.EndArray) { this.DeserializeValue(reader, document, manager, propertyName, xmlElement); num++; } if (this.WriteArrayAttribute) { this.AddJsonArrayAttribute(xmlElement, document); } if (num == 1 && this.WriteArrayAttribute) { foreach (IXmlNode childNode in xmlElement.ChildNodes) { IXmlElement xmlElement1 = childNode as IXmlElement; if (xmlElement1 == null || !(xmlElement1.LocalName == propertyName)) { continue; } this.AddJsonArrayAttribute(xmlElement1, document); return; } } }
private void CreateElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string elementName, XmlNamespaceManager manager, string elementPrefix, Dictionary <string, string> attributeNameValues) { IXmlElement xmlElement = this.CreateElement(elementName, document, elementPrefix, manager); currentNode.AppendChild(xmlElement); foreach (KeyValuePair <string, string> attributeNameValue in attributeNameValues) { string str = XmlConvert.EncodeName(attributeNameValue.Key); string prefix = MiscellaneousUtils.GetPrefix(attributeNameValue.Key); xmlElement.SetAttributeNode((!string.IsNullOrEmpty(prefix) ? document.CreateAttribute(str, manager.LookupNamespace(prefix) ?? string.Empty, attributeNameValue.Value) : document.CreateAttribute(str, attributeNameValue.Value))); } if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer || reader.TokenType == JsonToken.Float || reader.TokenType == JsonToken.Boolean || reader.TokenType == JsonToken.Date) { string xmlValue = this.ConvertTokenToXmlValue(reader); if (xmlValue != null) { xmlElement.AppendChild(document.CreateTextNode(xmlValue)); return; } } else if (reader.TokenType != JsonToken.Null) { if (reader.TokenType != JsonToken.EndObject) { manager.PushScope(); this.DeserializeNode(reader, document, manager, xmlElement); manager.PopScope(); } manager.RemoveNamespace(string.Empty, manager.DefaultNamespace); } }
/// <summary> /// Appends all elements of enumeration into specified document. They need to implement at least <see cref="IXmlConvertible"/>. /// </summary> public static int Append(IXmlDocument doc, XmlNode node, IEnumerator items, string comment, bool printComment, string childNodeName, string childNodeAttribute) { int counter = 0; // add comment: if (printComment) { node.AppendChild(doc.CreateComment(comment)); } // go throu all the elements and serialize them: if (items != null) { items.Reset(); while (items.MoveNext()) { XmlNode pElem = doc.CreateElement(childNodeName); AddAttribute(doc, pElem, childNodeAttribute, items.Current.ToString()); node.AppendChild(pElem); counter++; } } return(counter); }
static IXmlNode GetNode(IXmlDocument xmldoc, string tagName) { IXmlNodeList nodes = GetNodes(xmldoc, tagName); IXmlNode node = GetNode(nodes, 0); return(node); }
private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager) { Dictionary <string, string> attributeNameValues = ReadAttributeElements(reader, manager); string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); IXmlElement element = CreateElement(propertyName, document, elementPrefix, manager); currentNode.AppendChild(element); // add attributes to newly created element foreach (KeyValuePair <string, string> nameValue in attributeNameValues) { string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key); IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix), nameValue.Value) : document.CreateAttribute(nameValue.Key, nameValue.Value); element.SetAttributeNode(attribute); } if (reader.TokenType == JsonToken.String) { element.AppendChild(document.CreateTextNode(reader.Value.ToString())); } else if (reader.TokenType == JsonToken.Integer) { element.AppendChild(document.CreateTextNode(XmlConvert.ToString((long)reader.Value))); } else if (reader.TokenType == JsonToken.Float) { element.AppendChild(document.CreateTextNode(XmlConvert.ToString((double)reader.Value))); } else if (reader.TokenType == JsonToken.Boolean) { element.AppendChild(document.CreateTextNode(XmlConvert.ToString((bool)reader.Value))); } else if (reader.TokenType == JsonToken.Date) { DateTime d = (DateTime)reader.Value; element.AppendChild(document.CreateTextNode(XmlConvert.ToString(d, DateTimeUtils.ToSerializationMode(d.Kind)))); } else if (reader.TokenType == JsonToken.Null) { // empty element. do nothing } else { // finished element will have no children to deserialize if (reader.TokenType != JsonToken.EndObject) { manager.PushScope(); DeserializeNode(reader, document, manager, element); manager.PopScope(); } } }
private void DeserializeNode(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, IXmlNode currentNode) { do { switch (reader.TokenType) { case JsonToken.PropertyName: if (currentNode.NodeType == XmlNodeType.Document && document.DocumentElement != null) { throw new JsonSerializationException("JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName."); } string propertyName = reader.Value.ToString(); reader.Read(); if (reader.TokenType == JsonToken.StartArray) { int count = 0; while (reader.Read() && reader.TokenType != JsonToken.EndArray) { DeserializeValue(reader, document, manager, propertyName, currentNode); count++; } if (count == 1 && WriteArrayAttribute) { IXmlElement arrayElement = currentNode.ChildNodes.CastValid <IXmlElement>().Single(n => n.LocalName == propertyName); AddJsonArrayAttribute(arrayElement, document); } } else { DeserializeValue(reader, document, manager, propertyName, currentNode); } break; case JsonToken.StartConstructor: string constructorName = reader.Value.ToString(); while (reader.Read() && reader.TokenType != JsonToken.EndConstructor) { DeserializeValue(reader, document, manager, constructorName, currentNode); } break; case JsonToken.Comment: currentNode.AppendChild(document.CreateComment((string)reader.Value)); break; case JsonToken.EndObject: case JsonToken.EndArray: return; default: throw new JsonSerializationException("Unexpected JsonToken when deserializing node: " + reader.TokenType); } } while (reader.TokenType == JsonToken.PropertyName || reader.Read()); // don't read if current token is a property. token was already read when parsing element attributes }
private IXmlElement CreateElement(string elementName, IXmlDocument document, string elementPrefix, XmlNamespaceManager manager) { string ns = string.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix); IXmlElement element = (!string.IsNullOrEmpty(ns)) ? document.CreateElement(elementName, ns) : document.CreateElement(elementName); return(element); }
public SiteMapping(String fileName) { _fileName = fileName; var parser = new XmlParser(); var content = File.Exists(fileName) ? File.ReadAllText(_fileName) : "<entries></entries>"; _xml = parser.ParseDocument(content); }
static void SetNodeValueString(string str, IXmlDocument xmldoc, IXmlNode node) { IXmlText textNode; int res = xmldoc.CreateTextNode(str, out textNode); ComFunctions.CheckHRESULT(res); AppendNode(node, (IXmlNode)textNode); }
public void TestSimpleParser() { SimpleParser parser = new SimpleParser(); IXmlDocument xmlDoc = parser.ParseXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-cache-config.xml"); Assert.IsNotNull(xmlDoc); Assert.AreEqual(xmlDoc.Name, "cache-config"); }
static IXmlElement CreateElement(IXmlDocument xmldoc, string elementName) { IXmlElement element; using (var hStrign_elementName = HSTRING.FromString(elementName)) ComFunctions.CheckHRESULT(xmldoc.CreateElement(hStrign_elementName, out element)); return(element); }
static IXmlNodeList GetNodes(IXmlDocument xmldoc, string tagName) { IXmlNodeList nodes; using (var hStrign_tagName = HSTRING.FromString(tagName)) ComFunctions.CheckHRESULT(xmldoc.GetElementsByTagName(hStrign_tagName, out nodes)); return(nodes); }
static void SetNodeValueString(string str, IXmlDocument xmldoc, IXmlNode node) { IXmlText textNode; using (var hStrign_str = HSTRING.FromString(str)) ComFunctions.CheckHRESULT(xmldoc.CreateTextNode(hStrign_str, out textNode)); AppendNode(node, (IXmlNode)textNode); }
private static void AddAttribute(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string attributeName, XmlNamespaceManager manager, string attributePrefix) { string qualifiedName = XmlConvert.EncodeName(attributeName); string str2 = reader.Value.ToString(); IXmlNode attribute = !string.IsNullOrEmpty(attributePrefix) ? document.CreateAttribute(qualifiedName, manager.LookupNamespace(attributePrefix), str2) : document.CreateAttribute(qualifiedName, str2); ((IXmlElement)currentNode).SetAttributeNode(attribute); }
private void AddJsonArrayAttribute(IXmlElement element, IXmlDocument document) { element.SetAttributeNode(document.CreateAttribute("json:Array", "http://james.newtonking.com/projects/json", "true")); if (element is XElementWrapper && element.GetPrefixOfNamespace("http://james.newtonking.com/projects/json") == null) { element.SetAttributeNode(document.CreateAttribute("xmlns:json", "http://www.w3.org/2000/xmlns/", "http://james.newtonking.com/projects/json")); } }
static void UpdateTemplate(IXmlDocument xmldoc, IPredefinedToastNotificationInfo info) { if (info.ToastTemplateType != ToastTemplateType.ToastGeneric) { return; } SetAttribute(xmldoc, "binding", "template", "ToastGeneric"); }
static void SetNodeValueString(string str, IXmlDocument xmldoc, IXmlNode node) { IXmlText textNode; int res = xmldoc.CreateTextNode(str, out textNode); ComFunctions.CheckHRESULT(res); IXmlNode textNodeAsNode = (IXmlNode)textNode; IXmlNode appendedChild; res = node.AppendChild(textNodeAsNode, out appendedChild); ComFunctions.CheckHRESULT(res); }
public XmlDocumentLoader(IXmlDocument document) { if (document == null) throw new ArgumentNullException(nameof(document)); Document = document; XmlReaderSettings = new XmlReaderSettings(); XmlReaderSettings.DtdProcessing = DtdProcessing.Parse; XmlWriterSettings = new XmlWriterSettings(); XmlWriterSettings.Indent = true; }
static void SetImageSrc(IXmlDocument xmldoc, string imagePath) { IXmlNodeList nodes; int res = xmldoc.GetElementsByTagName("image", out nodes); ComFunctions.CheckHRESULT(res); IXmlNode imageNode; res = nodes.Item(0, out imageNode); ComFunctions.CheckHRESULT(res); IXmlNode srcAttribute; res = imageNode.Attributes.GetNamedItem("src", out srcAttribute); ComFunctions.CheckHRESULT(res); SetNodeValueString(imagePath, xmldoc, srcAttribute); }
private void ParseDom(IXmlDocument dom) { var root = dom.DocumentElement; if (root == null) return; switch (root.LocalName) { case "score-partwise": ParsePartwise(root); break; case "score-timewise": //ParseTimewise(root); break; default: throw new UnsupportedFormatException(); } }
public virtual bool Save(IXmlDocument document, Stream stream) { if (!stream.CanWrite) throw new NotSupportedException("The stream cannot be written to"); try { var xdoc = LoadXDocument(stream); using (var xmlWriter = XmlWriter.Create(stream, XmlWriterSettings)) { Document.Export(xdoc); xdoc.Save(xmlWriter); } return true; } catch (Exception ex) { throw; } }
private void ParseDom(IXmlDocument dom) { var root = dom.DocumentElement; if (root == null) return; // the XML uses IDs for referring elements within the // model. Therefore we do the parsing in 2 steps: // - at first we read all model elements and store them by ID in a lookup table // - after that we need to join up the information. if (root.LocalName == "GPIF") { Score = new Score(); // parse all children root.IterateChildren(n => { if (n.NodeType == XmlNodeType.Element) { switch (n.LocalName) { case "Score": ParseScoreNode(n); break; case "MasterTrack": ParseMasterTrackNode(n); break; case "Tracks": ParseTracksNode(n); break; case "MasterBars": ParseMasterBarsNode(n); break; case "Bars": ParseBars(n); break; case "Voices": ParseVoices(n); break; case "Beats": ParseBeats(n); break; case "Notes": ParseNotes(n); break; case "Rhythms": ParseRhythms(n); break; } } }); } else { throw new UnsupportedFormatException(); } BuildModel(); }
private IXmlElement CreateElement(string elementName, IXmlDocument document, string elementPrefix, XmlNamespaceManager manager) { return (!string.IsNullOrEmpty(elementPrefix)) ? document.CreateElement(elementName, manager.LookupNamespace(elementPrefix)) : document.CreateElement(elementName); }
private void CreateDocumentType(JsonReader reader, IXmlDocument document, IXmlNode currentNode) { string name = null; string publicId = null; string systemId = null; string internalSubset = null; while (reader.Read() && reader.TokenType != JsonToken.EndObject) { switch (reader.Value.ToString()) { case "@name": reader.Read(); name = reader.Value.ToString(); break; case "@public": reader.Read(); publicId = reader.Value.ToString(); break; case "@system": reader.Read(); systemId = reader.Value.ToString(); break; case "@internalSubset": reader.Read(); internalSubset = reader.Value.ToString(); break; default: throw new JsonSerializationException("Unexpected property name encountered while deserializing XmlDeclaration: " + reader.Value); } } IXmlNode documentType = document.CreateXmlDocumentType(name, publicId, systemId, internalSubset); currentNode.AppendChild(documentType); }
private static void AddAttribute(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, string attributeName, XmlNamespaceManager manager, string attributePrefix) { if (currentNode.NodeType == XmlNodeType.Document) { throw JsonSerializationException.Create(reader, "JSON root object has property '{0}' that will be converted to an attribute. A root object cannot have any attribute properties. Consider specifing a DeserializeRootElementName.".FormatWith(CultureInfo.InvariantCulture, propertyName)); } string encodedName = XmlConvert.EncodeName(attributeName); string attributeValue = reader.Value.ToString(); IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(encodedName, manager.LookupNamespace(attributePrefix), attributeValue) : document.CreateAttribute(encodedName, attributeValue); ((IXmlElement)currentNode).SetAttributeNode(attribute); }
private void AddJsonArrayAttribute(IXmlElement element, IXmlDocument document) { element.SetAttributeNode(document.CreateAttribute("json:Array", JsonNamespaceUri, "true")); }
private void ReadArrayElements(JsonReader reader, IXmlDocument document, string propertyName, IXmlNode currentNode, XmlNamespaceManager manager) { string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); IXmlElement nestedArrayElement = CreateElement(propertyName, document, elementPrefix, manager); currentNode.AppendChild(nestedArrayElement); int count = 0; while (reader.Read() && reader.TokenType != JsonToken.EndArray) { DeserializeValue(reader, document, manager, propertyName, nestedArrayElement); count++; } if (WriteArrayAttribute) { AddJsonArrayAttribute(nestedArrayElement, document); } if (count == 1 && WriteArrayAttribute) { foreach (IXmlNode childNode in nestedArrayElement.ChildNodes) { IXmlElement element = childNode as IXmlElement; if (element != null && element.LocalName == propertyName) { AddJsonArrayAttribute(element, document); break; } } } }
static void SetSound(IXmlDocument xmldoc, PredefinedSound sound) { string soundXml = "ms-winsoundevent:" + sound.ToString().Replace("_", "."); IXmlElement soundElement; ComFunctions.CheckHRESULT(xmldoc.CreateElement("audio", out soundElement)); if(sound == PredefinedSound.NoSound) { ComFunctions.CheckHRESULT(soundElement.SetAttribute("silent", "true")); } else { ComFunctions.CheckHRESULT(soundElement.SetAttribute("src", soundXml)); ComFunctions.CheckHRESULT(soundElement.SetAttribute("loop", IsLoopingSound(sound).ToString().ToLower())); } var asNode = (IXmlNode)xmldoc; IXmlNode appendedChild; IXmlNodeList nodes; ComFunctions.CheckHRESULT(xmldoc.GetElementsByTagName("toast", out nodes)); IXmlNode toastNode; ComFunctions.CheckHRESULT(nodes.Item(0, out toastNode)); ComFunctions.CheckHRESULT(toastNode.AppendChild((IXmlNode)soundElement, out appendedChild)); }
private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager) { Dictionary<string, string> attributeNameValues = ReadAttributeElements(reader, manager); string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); IXmlElement element = CreateElement(propertyName, document, elementPrefix, manager); currentNode.AppendChild(element); // add attributes to newly created element foreach (KeyValuePair<string, string> nameValue in attributeNameValues) { string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key); IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix), nameValue.Value) : document.CreateAttribute(nameValue.Key, nameValue.Value); element.SetAttributeNode(attribute); } if (reader.TokenType == JsonToken.String) { element.AppendChild(document.CreateTextNode(reader.Value.ToString())); } else if (reader.TokenType == JsonToken.Integer) { element.AppendChild(document.CreateTextNode(XmlConvert.ToString((long)reader.Value))); } else if (reader.TokenType == JsonToken.Float) { element.AppendChild(document.CreateTextNode(XmlConvert.ToString((double)reader.Value))); } else if (reader.TokenType == JsonToken.Boolean) { element.AppendChild(document.CreateTextNode(XmlConvert.ToString((bool)reader.Value))); } else if (reader.TokenType == JsonToken.Date) { DateTime d = (DateTime)reader.Value; element.AppendChild(document.CreateTextNode(XmlConvert.ToString(d, DateTimeUtils.ToSerializationMode(d.Kind)))); } else if (reader.TokenType == JsonToken.Null) { // empty element. do nothing } else { // finished element will have no children to deserialize if (reader.TokenType != JsonToken.EndObject) { manager.PushScope(); DeserializeNode(reader, document, manager, element); manager.PopScope(); } } }
// Token: 0x060006E6 RID: 1766 // RVA: 0x000385A8 File Offset: 0x000367A8 private void ReadArrayElements(JsonReader reader, IXmlDocument document, string propertyName, IXmlNode currentNode, XmlNamespaceManager manager) { string prefix = MiscellaneousUtils.GetPrefix(propertyName); IXmlElement xmlElement = this.CreateElement(propertyName, document, prefix, manager); currentNode.AppendChild(xmlElement); int num = 0; while (reader.Read()) { if (reader.TokenType == JsonToken.EndArray) { break; } this.DeserializeValue(reader, document, manager, propertyName, xmlElement); num++; } if (this.WriteArrayAttribute) { this.AddJsonArrayAttribute(xmlElement, document); } if (num == 1 && this.WriteArrayAttribute) { IXmlElement element = Enumerable.Single<IXmlElement>(Enumerable.OfType<IXmlElement>(xmlElement.ChildNodes), (IXmlElement n) => n.LocalName == propertyName); this.AddJsonArrayAttribute(element, document); } }
private IXmlElement CreateElement(string elementName, IXmlDocument document, string elementPrefix, XmlNamespaceManager manager) { string ns = string.IsNullOrEmpty(elementPrefix) ? manager.DefaultNamespace : manager.LookupNamespace(elementPrefix); IXmlElement element = (!string.IsNullOrEmpty(ns)) ? document.CreateElement(elementName, ns) : document.CreateElement(elementName); return element; }
private void AddJsonArrayAttribute(IXmlElement element, IXmlDocument document) { element.SetAttributeNode(document.CreateAttribute("json:Array", JsonNamespaceUri, "true")); #if !NET20 // linq to xml doesn't automatically include prefixes via the namespace manager if (element is XElementWrapper) { if (element.GetPrefixOfNamespace(JsonNamespaceUri) == null) { element.SetAttributeNode(document.CreateAttribute("xmlns:json", "http://www.w3.org/2000/xmlns/", JsonNamespaceUri)); } } #endif }
private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager) { if (string.IsNullOrEmpty(propertyName)) throw new JsonSerializationException("XmlNodeConverter cannot convert JSON with an empty property name to XML."); Dictionary<string, string> attributeNameValues = ReadAttributeElements(reader, manager); string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); if (propertyName.StartsWith("@")) { var attributeName = propertyName.Substring(1); var attributeValue = reader.Value.ToString(); var attributePrefix = MiscellaneousUtils.GetPrefix(attributeName); var attribute = (!string.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(attributeName, manager.LookupNamespace(attributePrefix), attributeValue) : document.CreateAttribute(attributeName, attributeValue); ((IXmlElement)currentNode).SetAttributeNode(attribute); } else { IXmlElement element = CreateElement(propertyName, document, elementPrefix, manager); currentNode.AppendChild(element); // add attributes to newly created element foreach (KeyValuePair<string, string> nameValue in attributeNameValues) { string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key); IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix), nameValue.Value) : document.CreateAttribute(nameValue.Key, nameValue.Value); element.SetAttributeNode(attribute); } if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer || reader.TokenType == JsonToken.Float || reader.TokenType == JsonToken.Boolean || reader.TokenType == JsonToken.Date) { element.AppendChild(document.CreateTextNode(ConvertTokenToXmlValue(reader))); } else if (reader.TokenType == JsonToken.Null) { // empty element. do nothing } else { // finished element will have no children to deserialize if (reader.TokenType != JsonToken.EndObject) { manager.PushScope(); DeserializeNode(reader, document, manager, element); manager.PopScope(); } } } }
static void SetTextLine(IXmlDocument xmldoc, uint index, string text) { IXmlNodeList nodes; ComFunctions.CheckHRESULT(xmldoc.GetElementsByTagName("text", out nodes)); Debug.Assert(nodes.Length >= index + 1); IXmlNode node; ComFunctions.CheckHRESULT(nodes.Item(index, out node)); SetNodeValueString(text, xmldoc, node); }
private void ReadArrayElements(JsonReader reader, IXmlDocument document, string propertyName, IXmlNode currentNode, XmlNamespaceManager manager) { string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); IXmlElement nestedArrayElement = CreateElement(propertyName, document, elementPrefix, manager); currentNode.AppendChild(nestedArrayElement); while (reader.Read() && reader.TokenType != JsonToken.EndArray) { DeserializeValue(reader, document, manager, propertyName, nestedArrayElement); } }
public List<FileNotFoundException> LoadFromXml(IXmlDocument document) { throw new Exception("The method or operation is not implemented."); }
static void SetDuration(IXmlDocument xmldoc, NotificationDuration duration) { IXmlNodeList nodes; ComFunctions.CheckHRESULT(xmldoc.GetElementsByTagName("toast", out nodes)); IXmlNode toastNode; ComFunctions.CheckHRESULT(nodes.Item(0, out toastNode)); ((IXmlElement)toastNode).SetAttribute("duration", "long"); }
private void DeserializeValue(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, string propertyName, IXmlNode currentNode) { switch (propertyName) { case TextName: currentNode.AppendChild(document.CreateTextNode(reader.Value.ToString())); break; case CDataName: currentNode.AppendChild(document.CreateCDataSection(reader.Value.ToString())); break; case WhitespaceName: currentNode.AppendChild(document.CreateWhitespace(reader.Value.ToString())); break; case SignificantWhitespaceName: currentNode.AppendChild(document.CreateSignificantWhitespace(reader.Value.ToString())); break; default: // processing instructions and the xml declaration start with ? if (!string.IsNullOrEmpty(propertyName) && propertyName[0] == '?') { CreateInstruction(reader, document, currentNode, propertyName); } else { if (reader.TokenType == JsonToken.StartArray) { // handle nested arrays ReadArrayElements(reader, document, propertyName, currentNode, manager); return; } // have to wait until attributes have been parsed before creating element // attributes may contain namespace info used by the element ReadElement(reader, document, currentNode, propertyName, manager); } break; } }
private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager) { if (string.IsNullOrEmpty(propertyName)) throw JsonSerializationException.Create(reader, "XmlNodeConverter cannot convert JSON with an empty property name to XML."); Dictionary<string, string> attributeNameValues = ReadAttributeElements(reader, manager); string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); if (propertyName.StartsWith('@')) { string attributeName = propertyName.Substring(1); string attributePrefix = MiscellaneousUtils.GetPrefix(attributeName); AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix); } else if (propertyName.StartsWith('$')) { if (propertyName == JsonTypeReflector.ArrayValuesPropertyName) { propertyName = propertyName.Substring(1); elementPrefix = manager.LookupPrefix(JsonNamespaceUri); CreateElement(reader, document, currentNode, propertyName, manager, elementPrefix, attributeNameValues); } else { string attributeName = propertyName.Substring(1); string attributePrefix = manager.LookupPrefix(JsonNamespaceUri); AddAttribute(reader, document, currentNode, attributeName, manager, attributePrefix); } } else { CreateElement(reader, document, currentNode, propertyName, manager, elementPrefix, attributeNameValues); } }
private void ReadArrayElements(JsonReader reader, IXmlDocument document, string propertyName, IXmlNode currentNode, XmlNamespaceManager manager) { string elementPrefix = MiscellaneousUtils.GetPrefix(propertyName); IXmlElement nestedArrayElement = CreateElement(propertyName, document, elementPrefix, manager); currentNode.AppendChild(nestedArrayElement); int count = 0; while (reader.Read() && reader.TokenType != JsonToken.EndArray) { DeserializeValue(reader, document, manager, propertyName, nestedArrayElement); count++; } if (WriteArrayAttribute) { AddJsonArrayAttribute(nestedArrayElement, document); } if (count == 1 && WriteArrayAttribute) { IXmlElement arrayElement = nestedArrayElement.ChildNodes.CastValid<IXmlElement>().Single(n => n.LocalName == propertyName); AddJsonArrayAttribute(arrayElement, document); } }
private void CreateElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string elementName, XmlNamespaceManager manager, string elementPrefix, Dictionary<string, string> attributeNameValues) { IXmlElement element = CreateElement(elementName, document, elementPrefix, manager); currentNode.AppendChild(element); // add attributes to newly created element foreach (KeyValuePair<string, string> nameValue in attributeNameValues) { string attributePrefix = MiscellaneousUtils.GetPrefix(nameValue.Key); IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(nameValue.Key, manager.LookupNamespace(attributePrefix) ?? string.Empty, nameValue.Value) : document.CreateAttribute(nameValue.Key, nameValue.Value); element.SetAttributeNode(attribute); } if (reader.TokenType == JsonToken.String || reader.TokenType == JsonToken.Integer || reader.TokenType == JsonToken.Float || reader.TokenType == JsonToken.Boolean || reader.TokenType == JsonToken.Date) { element.AppendChild(document.CreateTextNode(ConvertTokenToXmlValue(reader))); } else if (reader.TokenType == JsonToken.Null) { // empty element. do nothing } else { // finished element will have no children to deserialize if (reader.TokenType != JsonToken.EndObject) { manager.PushScope(); DeserializeNode(reader, document, manager, element); manager.PopScope(); } manager.RemoveNamespace(string.Empty, manager.DefaultNamespace); } }
private void CreateInstruction(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName) { if (propertyName == DeclarationName) { string version = null; string encoding = null; string standalone = null; while (reader.Read() && reader.TokenType != JsonToken.EndObject) { switch (reader.Value.ToString()) { case "@version": reader.Read(); version = reader.Value.ToString(); break; case "@encoding": reader.Read(); encoding = reader.Value.ToString(); break; case "@standalone": reader.Read(); standalone = reader.Value.ToString(); break; default: throw new JsonSerializationException("Unexpected property name encountered while deserializing XmlDeclaration: " + reader.Value); } } IXmlNode declaration = document.CreateXmlDeclaration(version, encoding, standalone); currentNode.AppendChild(declaration); } else { IXmlNode instruction = document.CreateProcessingInstruction(propertyName.Substring(1), reader.Value.ToString()); currentNode.AppendChild(instruction); } }
private static void AddAttribute(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string attributeName, XmlNamespaceManager manager, string attributePrefix) { string attributeValue = reader.Value.ToString(); IXmlNode attribute = (!string.IsNullOrEmpty(attributePrefix)) ? document.CreateAttribute(attributeName, manager.LookupNamespace(attributePrefix), attributeValue) : document.CreateAttribute(attributeName, attributeValue); ((IXmlElement) currentNode).SetAttributeNode(attribute); }
private void DeserializeNode(JsonReader reader, IXmlDocument document, XmlNamespaceManager manager, IXmlNode currentNode) { do { switch (reader.TokenType) { case JsonToken.PropertyName: if (currentNode.NodeType == XmlNodeType.Document && document.DocumentElement != null) throw new JsonSerializationException("JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifing a DeserializeRootElementName."); string propertyName = reader.Value.ToString(); reader.Read(); if (reader.TokenType == JsonToken.StartArray) { int count = 0; while (reader.Read() && reader.TokenType != JsonToken.EndArray) { DeserializeValue(reader, document, manager, propertyName, currentNode); count++; } if (count == 1 && WriteArrayAttribute) { IXmlElement arrayElement = currentNode.ChildNodes.CastValid<IXmlElement>().Single(n => n.LocalName == propertyName); AddJsonArrayAttribute(arrayElement, document); } } else { DeserializeValue(reader, document, manager, propertyName, currentNode); } break; case JsonToken.StartConstructor: string constructorName = reader.Value.ToString(); while (reader.Read() && reader.TokenType != JsonToken.EndConstructor) { DeserializeValue(reader, document, manager, constructorName, currentNode); } break; case JsonToken.Comment: currentNode.AppendChild(document.CreateComment((string)reader.Value)); break; case JsonToken.EndObject: case JsonToken.EndArray: return; default: throw new JsonSerializationException("Unexpected JsonToken when deserializing node: " + reader.TokenType); } } while (reader.TokenType == JsonToken.PropertyName || reader.Read()); // don't read if current token is a property. token was already read when parsing element attributes }
// Token: 0x060006E4 RID: 1764 // RVA: 0x000382C8 File Offset: 0x000364C8 private void ReadElement(JsonReader reader, IXmlDocument document, IXmlNode currentNode, string propertyName, XmlNamespaceManager manager) { if (string.IsNullOrEmpty(propertyName)) { throw new JsonSerializationException("XmlNodeConverter cannot convert JSON with an empty property name to XML."); } Dictionary<string, string> dictionary = this.ReadAttributeElements(reader, manager); string prefix = MiscellaneousUtils.GetPrefix(propertyName); if (StringUtils.StartsWith(propertyName, '@')) { string text = propertyName.Substring(1); string value = reader.Value.ToString(); string prefix2 = MiscellaneousUtils.GetPrefix(text); IXmlNode attributeNode = (!string.IsNullOrEmpty(prefix2)) ? document.CreateAttribute(text, manager.LookupNamespace(prefix2), value) : document.CreateAttribute(text, value); ((IXmlElement)currentNode).SetAttributeNode(attributeNode); return; } IXmlElement xmlElement = this.CreateElement(propertyName, document, prefix, manager); currentNode.AppendChild(xmlElement); foreach (KeyValuePair<string, string> current in dictionary) { string prefix3 = MiscellaneousUtils.GetPrefix(current.Key); IXmlNode attributeNode2 = (!string.IsNullOrEmpty(prefix3)) ? document.CreateAttribute(current.Key, manager.LookupNamespace(prefix3), current.Value) : document.CreateAttribute(current.Key, current.Value); xmlElement.SetAttributeNode(attributeNode2); } if (reader.TokenType != JsonToken.String && reader.TokenType != JsonToken.Integer && reader.TokenType != JsonToken.Float && reader.TokenType != JsonToken.Boolean) { if (reader.TokenType != JsonToken.Date) { if (reader.TokenType == JsonToken.Null) { return; } if (reader.TokenType != JsonToken.EndObject) { manager.PushScope(); this.DeserializeNode(reader, document, manager, xmlElement); manager.PopScope(); } manager.RemoveNamespace(string.Empty, manager.DefaultNamespace); return; } } xmlElement.AppendChild(document.CreateTextNode(this.ConvertTokenToXmlValue(reader))); }