Changing a property value when converting from XML to JSON

In the project, I get some data from the database using TSQL FOR XML. In all tables, the minimun value for Date fields is set to 01/01/1900.

This is because null values ​​are not allowed as an internal policy that cannot be changed.

With the result of xml, I need to convert it to JSON and serialize it to the client.

For this, I use:

string jSonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None, true);

Good; Now I need to convert each date 01/01/1900to DateTime.MinValue(01/01/0001) when converting to JSON; how can i handle this?

+4
source share
2 answers

UPDATE - Changed code to work for any node / attribute that matters 01/01/1900.

XML- JSON. LinqPad, , node . XML, :

void Main()
{
    // IF IT IS NODE VALUE
    var xml = @"<data>
        <objectA>
            <dateValueA>01/01/1900</dateValueA>
            <dateValueB>01/01/1971</dateValueB>
        </objectA>
        <objectB>
            <dateValueA>01/01/2002</dateValueA>
            <dateValueB>01/01/1900</dateValueB>
            <dateValueZ>01/01/2011</dateValueZ>
        </objectB>
        <objectC>
            <dateValueA>01/01/1910</dateValueA>
            <dateValueB>01/01/2012</dateValueB>
            <dateValueC>01/01/1900</dateValueC>
        </objectC>
    </data>";

    var xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);

    var nodes = xmlDoc.SelectNodes("//*[text()='01/01/1900']");
    foreach(XmlNode node in nodes)
    {
        node.InnerText = "01/01/0001";
    }

    string jSonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None, true);

    "// IF IT IS NODE VALUE - RESULTS".Dump();
    jSonString.Dump();

    // IF IT IS ATTRIBUTE VALUE
    var xmlAttr = @"<data>
        <objectA dateValueA='01/01/1900' dateValueB='01/01/1900' dateValueC='01/01/2011' />
        <objectB dateValueB='01/01/2011' someOtherDate='01/01/1900' />
        <objectC dateValueC='01/01/1900' dontChangeThisDate='05/04/1923' />
    </data>";

    var xmlDocAttr = new XmlDocument();
    xmlDoc.LoadXml(xmlAttr);

    var nodesAttr = xmlDoc.SelectNodes("//*[@*='01/01/1900']");
    for(var i=0; i < nodesAttr.Count; i++)
    {
        foreach(XmlAttribute attrib in nodesAttr[i].Attributes)
        {
            if (attrib.Value == "01/01/1900")
            {
                attrib.Value = "01/01/0001";
            }
        }
    }

    string jSonStringAttr = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None, true);

    "// IF IT IS ATTRIBUTE VALUE - RESULTS".Dump();
    jSonStringAttr.Dump();

}

:

// IF IT IS NODE VALUE - RESULTS
{"objectA":{"dateValueA":"01/01/0001","dateValueB":"01/01/1971"},"objectB":{"dateValueA":"01/01/2002","dateValueB":"01/01/0001","dateValueZ":"01/01/2011"},"objectC":{"dateValueA":"01/01/1910","dateValueB":"01/01/2012","dateValueC":"01/01/0001"}}

// IF IT IS ATTRIBUTE VALUE - RESULTS
{"objectA":{"@dateValueA":"01/01/0001","@dateValueB":"01/01/0001","@dateValueC":"01/01/2011"},"objectB":{"@dateValueB":"01/01/2011","@someOtherDate":"01/01/0001"},"objectC":{"@dateValueC":"01/01/0001","@dontChangeThisDate":"05/04/1923"}}

?

+1

T-SQL, :

CASE   
    WHEN [DateWhichCantBeNull] = '1900-01-01'
        THEN '0001-01-01'
    ELSE [DateWhichCantBeNull]
END AS "DateWhichCantBeNull"

DateTime2, .

0

All Articles