Last post Jan 23, 2019 01:54 PM by jzero
Jan 23, 2019 10:11 AM|AnyUserNameThatLetsMeIn|LINK
I have a small application which picks up data from an external source and loads it into an object. I use this object throughout the application. However, during the general usage of the application, I decided that I needed to dump the object out to an XML
file to store it for logging purposes. To do that I knocked up a small function to dump the object to an XML string which I write to a file. The function that does it takes the object as a parameter and does this:
private static string XMLBDataToString(bDEntity bData)
Utf8StringWriter stringWriter = new Utf8StringWriter();
XmlSerializer serialiser = new XmlSerializer(bData.GetType());
For reference, the Utf8StringWriter is just a normal stringwriter where I have overridden the encoding because I found it defaulted to UTF16 which I didn't want.
public class Utf8StringWriter : System.IO.StringWriter
public override Encoding Encoding => Encoding.UTF8;
It's quick and dirty, probably not the best way or right way to do it, but it works. I want to clean it up a bit and make it do a little more. The class bDEntity is quite a large class with many partial classes and hundreds of properties. When I dump out
that object to XML using the above function it creates quite a complex XML document. That is fine. However, where no value is set in the property, it gives empty nodes, for example:
There are hundreds of these empty nodes. Now, flipping over to a different project I worked on, I used JSON. And before we go any further, no I am not wanting to change this so it outputs JSON instead of XML. When I work with JSON data I use the Newtonsoft
Json.NET Nuget package. One of the things you can do with that when you dump an object out to JSON is to pass formatting and JsonSerializerSettings to the SerializeObject function to do things like this:
string json = JsonConvert.SerializeObject(aRIBatch, Formatting.Indented, new JsonSerializerSettings
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore
So in this case, when I dump the object to a JSON string, it ignores null values and default values which makes a large object, quite a small JSON string. So the question is, is there a way to dump the object to an XML document, but only dump the values
that hold data and ignore null value properties and properties with default values in the same way I can with Json.NET?
I thought maybe instead of the XMLBDataToString function, I could use Json.NET to dump the object to JSON using the above formatting and then convert it to XML but I wan't sure if that was a good way to do it. If it is, then I could do with a bit of guidance.
Jan 23, 2019 01:54 PM|jzero|LINK
I think an alternative would work with ShouldSerialize, but this means you have to make some changes on your bDEntity, at same time such change could be a problem if it is dynamically generated like DBML or EF.
On the other hand with help of Linq To XML, you can have a XDocument with all empty nodes removed.
This can not be an elegant way, but should help you
StringBuilder StringXML = new StringBuilder();
// Load XML string into XDocument
XDocument myXDoc = XDocument.Parse(StringXML.ToString());
// Remove all empty nodes
myXDoc.Descendants().Where(x => string.IsNullOrEmpty(x.Value)).Remove(); // This should remove <code></code> and <code />
// If needd to convert XDocument to XMLDocument
XmlDocument XMLDoc = new XmlDocument();