using System.Xml.Serialization;
using System.Collections.Generic;
[XmlType(Namespace ="http://www.cpandl.com")]
[XmlElement(Namespace = "http://www.prop1.com")]
public string PROP1 { get; set; }
[XmlElement(Namespace = "http://www.prop3.com")]
public string PROP3 { get; set; }
public static void Main()
var myNamespaces = new XmlSerializerNamespaces();
myNamespaces.Add("books", "http://www.prop1.com");
myNamespaces.Add("money", "http://www.prop3.com");
myNamespaces.Add("money2", "http://www.cpandl.com");
var writer = new StringWriter();
new XmlSerializer(typeof(XmlString)).Serialize(writer, obj, myNamespaces);
Console.WriteLine(writer.ToString());
""DOCUMENTCODE"": ""TST123456789"",
""ORGANIZATIONCODE"": ""GSO""
""DESCRIPTION"": ""TESTING STUFF""
""FILE"": ""TESTING1234.pdf"",
""CLASSCODE"": ""UTSUPLD"",
""ORGANIZATIONCODE"": ""*""
""PROPERTYCODE"": ""UTSID"",
""CLASSCODE"": ""UTSUPLD""
""TEXTFIELD"": ""1234567"",
""PROPERTYCODE"": ""UTSFORM"",
""CLASSCODE"": ""UTSUPLD""
""TEXTFIELD"": ""TEST FORM"",
""PROPERTYCODE"": ""UTSWO"",
""CLASSCODE"": ""UTSUPLD""
""TEXTFIELD"": ""123456"",
var json = JsonDocument.Parse(jsonStr).RootElement;
var documentCode = json.GetProperty("CLASSID").TryGetProperty("YEAR", out var str);
Console.WriteLine(documentCode);
var numberString = $"{sign}{num[0..(num.Length - decimals)]}.{num[(num.Length - decimals + 1)..]}";
var didParse = double.TryParse(numberString, out var dbl);
Console.WriteLine(dbl + 100000);
var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(jsonStr));
var doc = DeserializeInternal(typeof(EamDocument), ref reader);
Console.WriteLine(JsonSerializer.Serialize(doc, new JsonSerializerOptions { WriteIndented = true} ));
public static object DeserializeInternal(Type type, ref Utf8JsonReader reader)
var obj = Activator.CreateInstance(type);
var dict = GetDeserializationProperties(obj.GetType());
if (reader.TokenType == JsonTokenType.PropertyName)
var propertyName = reader.GetString();
Console.WriteLine(propertyName);
if (dict.TryGetValue(propertyName!, out var property))
if (reader.TokenType == JsonTokenType.String)
property.SetValue(obj, reader.GetString());
else if (reader.TokenType == JsonTokenType.StartArray)
if (reader.TokenType == JsonTokenType.StartObject) {
var subType = property.PropertyType;
var subObj = Activator.CreateInstance(subType);
private static IDictionary<string, PropertyInfo> GetDeserializationProperties(Type type) {
var properties = type.GetProperties()
.Where(prop => prop.IsDefined(typeof(EamPropertyAttribute), false));
var dict = new Dictionary<string, PropertyInfo>();
foreach (var item in properties) {
var attrs = item.GetCustomAttributes(false).Where(attr => attr is EamPropertyAttribute a && !a.DeserializeIgnore).FirstOrDefault();
if (attrs == null) continue;
dict.Add(((EamPropertyAttribute)attrs).Property, item);
[EamProperty("DOCUMENTCODE", "DOCUMENTID")]
public string DocumentCode { get; set; }
[EamProperty("DESCRIPTION", "DOCUMENTID")]
public string Description { get; set; }
[EamProperty("CLASSCODE", "CLASSID")]
public string Class { get; set; }
[EamProperty("ORGANIZATIONCODE", "DOCUMENTID/ORGANIZATIONID")]
public string Organization { get; set; }
public string File { get; set; }
[EamProperty("entity", DeserializeIgnore = true)]
public string Entity { get; set; } = "DOCU";
[EamProperty("TYPECODE", "DOCUMENTTYPE", DeserializeIgnore = true)]
public string DocumentType { get; set; } = "U";
[EamProperty("CUSTOMFIELD", "USERDEFINEDAREA", DictionaryKeyType = typeof(string), DictionaryValueType = typeof(CustomField))]
public Dictionary<string, CustomField> CustomFields { get; set; }
public EamDocument(string documentCode, string description, string organization, string file, string documentClass, Dictionary<string, CustomField> customFields)
DocumentCode = documentCode;
Description = description;
Organization = organization;
CustomFields = customFields ?? new Dictionary<string, CustomField>();
public EamDocument(string documentCode, string description, string organization, string file, string documentClass) :
this (documentCode, description, file, organization, documentClass, null) { }
[EamProperty("PROPERTYCODE")]
public string Property { get; set; }
[EamProperty("PROPERTYLABEL")]
public string PropertyLabel { get; set; }
[EamProperty("CLASSCODE")]
public string Class { get; set; }
[EamProperty("ORGANIZATIONCODE")]
public string ClassOrganization { get; set; }
public object Value { get; set; }
[EamProperty("MAXVALUE")]
public string MaxValue { get; set; }
[EamProperty("MINVALUE")]
public string MinValue { get; set; }
public string Uom { get; set; }
public bool IsLovField { get; set; }
[EamProperty("LOVVALIDATE")]
public bool IsLovValidatable { get; set; }
[EamProperty("GROUPLABEL")]
public string GroupLabel { get; set; }
public int Index { get; set; }
public string Type { get; set; }
public CustomField () { }
public CustomField (string property, string propertyClass, string classOrganization, object value, string type)
ClassOrganization = classOrganization;
class EamPropertyAttribute : Attribute
public string Property { get; set; }
public string Path { get; set; }
public bool DeserializeIgnore { get; set; }
public Type DictionaryKeyType { get; set; }
public Type DictionaryValueType { get; set; }
public Type ListType { get; set; }
public EamPropertyAttribute(string property, string path)
public EamPropertyAttribute(string property)