I’m converting a JSON file from jsonrpc to XML.
But the API gives fields with false
values when the field is not filled in, e.g. street2
{
"jsonrpc": "2.0",
"id": null,
"code": 200,
"result": [
{
"id": 32,
"name": "Somebody",
"street": "21 golfcountry",
"street2": false,
"city": "Amsterdam",
"custom_value_1": "false",
"custom_value_2": "true",
"custom_value_3": "sometimes",
"is_company": false
},
{
"id": 38,
"name": "Somebody else",
"street": "27 golfcountry",
"street2": "sub lane 21",
"city": "Amsterdam",
"custom_value_1": "",
"custom_value_2": "10 %",
"custom_value_3": "always",
"is_company": true
}
]
}
so the XML resolves in
<street2>false</street2>
and
<street2>sub lane 21</street2>
I want to remove those false fields before converting.
In this case ‘false’ can be an custom text value, but can be a logic.
- Remove: “street2”: false
- Leave: “custom_value_1”: “false”
- Remove: “is_company”: false
How can i remove those false values in 1 + 3
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
16
Here is a solution that is using XSLT to remove not needed XML elements as a post-processing step.
It is using a so called Identity Transform XSLT pattern.
The 2nd XSLT template removing two XML elements if their textual value is false.
Input XML
<root>
<jsonrpc>2.0</jsonrpc>
<code>200</code>
<result>
<id>32</id>
<name>Somebody</name>
<street2>false</street2>
<city>Amsterdam</city>
<is_company>false</is_company>
</result>
<result>
<id>38</id>
<name>Somebody else</name>
<street2>sub lane 21</street2>
<city>Amsterdam</city>
</result>
</root>
XSLT 1.0
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"
encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!--Identity transform-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="street2 | is_company [text()='false']" />
</xsl:stylesheet>
Output XML
<root>
<jsonrpc>2.0</jsonrpc>
<code>200</code>
<result>
<id>32</id>
<name>Somebody</name>
<city>Amsterdam</city>
</result>
<result>
<id>38</id>
<name>Somebody else</name>
<street2>sub lane 21</street2>
<city>Amsterdam</city>
</result>
</root>
c#
void Main()
{
const string SOURCEXMLFILE = @"e:Tempinput.xml";
const string XSLTFILE = @"e:Tempprocess.xslt";
const string OUTPUTXMLFILE = @"e:tempoutput.xml";
try
{
XsltArgumentList xslArg = new XsltArgumentList();
using (XmlReader src = XmlReader.Create(SOURCEXMLFILE))
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XSLTFILE, new XsltSettings(true, true), new XmlUrlResolver());
XmlWriterSettings settings = xslt.OutputSettings.Clone();
settings.IndentChars = "t";
// to remove BOM
settings.Encoding = new UTF8Encoding(false);
using (XmlWriter result = XmlWriter.Create(OUTPUTXMLFILE, settings))
{
xslt.Transform(src, xslArg, result, new XmlUrlResolver());
result.Close();
}
}
Console.WriteLine("File '{0}' has been generated.", OUTPUTXMLFILE);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
4