private void GetXml(StringBuilder msg, object value, string key, ChoNullValueHandling nullValueHandling, string nsPrefix = null, bool isCDATA = false, bool emitDataType = false) { if (value is ChoDynamicObject) { msg.AppendFormat("{0}{1}", Environment.NewLine, ((ChoDynamicObject)value).GetXml(((ChoDynamicObject)value).NName, nullValueHandling, nsPrefix).Indent(1, " ")); } else { if (value != null) { if (value.GetType().IsSimple()) { if (isCDATA) { msg.AppendFormat("{0}{1}", Environment.NewLine, "<{0}><![CDATA[{1}]]></{0}>".FormatString(key, value).Indent(1, " ")); } else { msg.AppendFormat("{0}{1}", Environment.NewLine, "<{0}>{1}</{0}>".FormatString(key, value).Indent(1, " ")); } } else { key = value is IList?key.ToPlural() != key?key.ToPlural() : key.Length > 1 && key.EndsWith("s", StringComparison.InvariantCultureIgnoreCase) ? key : "{0}s".FormatString(key) : key; msg.AppendFormat("{0}{1}", Environment.NewLine, "<{0}>".FormatString(key).Indent(1, " ")); if (value is IList) { foreach (var collValue in ((IList)value).OfType <ChoDynamicObject>()) { msg.AppendFormat("{0}{1}", Environment.NewLine, collValue.GetXml(collValue.NName == DefaultName ? key.ToSingular() : collValue.NName, nullValueHandling, nsPrefix).Indent(2, " ")); } } else { msg.AppendFormat("{0}{1}", Environment.NewLine, ChoUtility.XmlSerialize(value).Indent(2, " ")); } msg.AppendFormat("{0}{1}", Environment.NewLine, "</{0}>".FormatString(key).Indent(1, " ")); } } else { switch (nullValueHandling) { case ChoNullValueHandling.Empty: msg.AppendFormat("{0}{1}", Environment.NewLine, @"<{0}/>".FormatString(key).Indent(1, " ")); break; case ChoNullValueHandling.Ignore: break; default: msg.AppendFormat("{0}{1}", Environment.NewLine, @"<{0} xsi:nil=""true"" xmlns:xsi=""{1}""/>".FormatString(key, ChoXmlSettings.XmlSchemaInstanceNamespace).Indent(1, " ")); break; } } } }
public ChoYamlWriter <T> NullValueHandling(ChoNullValueHandling value = ChoNullValueHandling.Default) { Configuration.NullValueHandling = value; return(this); }
public static dynamic ToDynamic(this XElement element, string xmlSchemaNS = null, string jsonSchemaNS = null, ChoEmptyXmlNodeValueHandling emptyXmlNodeValueHandling = ChoEmptyXmlNodeValueHandling.Null, bool retainXmlAttributesAsNative = true, ChoNullValueHandling nullValueHandling = ChoNullValueHandling.Ignore) { // loop through child elements // define an Expando Dynamic ChoDynamicObject obj = new ChoDynamicObject(element.Name.LocalName); bool hasAttr = false; // cater for attributes as properties if (element.HasAttributes(xmlSchemaNS, jsonSchemaNS)) { foreach (var attribute in element.Attributes()) { if (!attribute.IsValidAttribute()) { continue; } hasAttr = true; obj.SetAttribute(attribute.Name.LocalName, System.Net.WebUtility.HtmlDecode(attribute.Value)); } } // cater for child nodes as properties, or child objects if (element.HasElements) { foreach (var kvp in element.Elements().GroupBy(e => e.Name.LocalName).Select(g => new { Name = g.Key, Value = g.ToArray() })) { if (kvp.Value.Length == 1 && !kvp.Value.First().IsJsonArray(jsonSchemaNS)) { XElement subElement = kvp.Value.First(); if (subElement.HasAttributes(xmlSchemaNS, jsonSchemaNS) || subElement.HasElements) { string keyName = null; object dobj = ToDynamic(subElement, xmlSchemaNS, jsonSchemaNS, emptyXmlNodeValueHandling, retainXmlAttributesAsNative, nullValueHandling); if (dobj != null || (dobj == null && emptyXmlNodeValueHandling != ChoEmptyXmlNodeValueHandling.Ignore)) { keyName = subElement.Name.LocalName; obj.SetElement(keyName, dobj, IsCDATANode(subElement)); } } else { if (subElement.IsNilElement()) { obj.SetElement(subElement.Name.LocalName, subElement.NilAwareValue(xmlSchemaNS), IsCDATANode(subElement)); } else { string value = System.Net.WebUtility.HtmlDecode(subElement.NilAwareValue(xmlSchemaNS)); if (value.IsNullOrEmpty() && !subElement.HasAttributes()) { switch (emptyXmlNodeValueHandling) { case ChoEmptyXmlNodeValueHandling.Empty: obj.SetElement(subElement.Name.LocalName, value, IsCDATANode(subElement)); break; case ChoEmptyXmlNodeValueHandling.Null: value = value.IsNullOrEmpty() ? null : value; obj.SetElement(subElement.Name.LocalName, value, IsCDATANode(subElement)); break; } } else { obj.SetElement(subElement.Name.LocalName, value, IsCDATANode(subElement)); } } } } else { if (kvp.Value.Length == 1) { XElement subElement2 = kvp.Value.First(); List <object> subDynamic = new List <object>(); foreach (XElement subsubElement in subElement2.Elements()) { var sd = ToDynamic(subsubElement, xmlSchemaNS, jsonSchemaNS, emptyXmlNodeValueHandling, retainXmlAttributesAsNative, nullValueHandling); if (sd != null || (sd == null && emptyXmlNodeValueHandling != ChoEmptyXmlNodeValueHandling.Ignore)) { subDynamic.Add(sd); } } obj.SetElement(subElement2.Name.LocalName, subDynamic.ToArray(), IsCDATANode(subElement2)); } else { List <object> list = new List <object>(); string keyName = null; foreach (var subElement in kvp.Value) { if (subElement == null) { continue; } object dobj = ToDynamic(subElement, xmlSchemaNS, jsonSchemaNS, emptyXmlNodeValueHandling, retainXmlAttributesAsNative, nullValueHandling); if (dobj != null || (dobj == null && emptyXmlNodeValueHandling != ChoEmptyXmlNodeValueHandling.Ignore)) { if (dobj is IList && ((IList)dobj).OfType <ChoDynamicObject>().Count() == ((IList)dobj).Count) { list.AddRange(((IList)dobj).Cast <object>()); } else { list.Add(dobj); } } keyName = subElement.Name.LocalName + "s"; } if (!hasAttr && obj.Count == 0) { return(list.ToArray()); } else { obj.SetElement(keyName, list.ToArray()); } } } } } else { if (element.IsNilElement()) { obj.SetText(System.Net.WebUtility.HtmlDecode(element.NilAwareValue(xmlSchemaNS))); } else { string value = System.Net.WebUtility.HtmlDecode(element.NilAwareValue(xmlSchemaNS)); if (value.IsNullOrEmpty()) { if (!element.HasAttributes()) { switch (emptyXmlNodeValueHandling) { case ChoEmptyXmlNodeValueHandling.Empty: obj.SetText(value); break; case ChoEmptyXmlNodeValueHandling.Null: obj.SetText(value.IsNullOrEmpty() ? null : value); break; default: return(null); } } else { switch (nullValueHandling) { case ChoNullValueHandling.Empty: obj.SetText(String.Empty); break; case ChoNullValueHandling.Null: obj.SetText(null); break; } } } else { obj.SetText(value); } } } return(obj); }
public string GetXml(string tag = null, ChoNullValueHandling nullValueHandling = ChoNullValueHandling.Empty, string nsPrefix = null, bool emitDataType = false) { if (nsPrefix.IsNullOrWhiteSpace()) { nsPrefix = String.Empty; } if (tag.IsNullOrWhiteSpace()) { tag = NName; } bool hasAttrs = false; StringBuilder msg = new StringBuilder("<{0}".FormatString(tag)); foreach (string key in this.Keys.Where(k => IsAttribute(k) && k != ValueToken)) { hasAttrs = true; msg.AppendFormat(@" {0}=""{1}""", key.Substring(1), this[key]); } if (ContainsKey(ValueToken)) { if (hasAttrs) { msg.AppendFormat(">"); msg.AppendFormat("{0}{1}", Environment.NewLine, this[ValueToken].ToNString().Indent(1, " ")); msg.AppendFormat("{0}</{1}>", Environment.NewLine, tag); } else { object value = this[ValueToken]; if (emitDataType) { } msg.AppendFormat(">"); msg.AppendFormat("{0}", value.ToNString()); msg.AppendFormat("</{0}>", tag); } } else if (this.Keys.Any(k => !IsAttribute(k))) { object value = null; msg.AppendFormat(">"); foreach (string key in this.Keys.Where(k => !IsAttribute(k))) { value = this[key]; var x = IsCDATA(key); GetXml(msg, value, key, nullValueHandling, nsPrefix, IsCDATA(key), emitDataType); } msg.AppendFormat("{0}</{1}>", Environment.NewLine, tag); } //else if (_list != null && _list.Count > 0) //{ // msg.AppendFormat(">"); // foreach (var obj in _list) // { // if (obj == null) continue; // GetXml(msg, obj, tag.ToSingular()); // } // msg.AppendFormat("{0}</{1}>", Environment.NewLine, tag); //} else { msg.AppendFormat(" />"); } return(msg.ToString()); }