Inject XML attribute into serialization - c#

I am trying to serialize an array and want to attach an attribute to the array. For example, the output I want is:
<ArrayOfThingie version="1.0">
<Thingie>
<name>one</name>
</Thingie>
<Thingie>
<name>two</name>
</Thingie>
</ArrayOfThingie>
This is just a primitive array, so I don't want to define the attribute for the array itself, just in its serialization. Is there a way to inject an attribute into the serialization?

You could create a wrapper for ArrayOfThingie just for serialization:
public class Thingie
{
[XmlElement("name")]
public string Name { get; set; }
}
[XmlRoot]
public class ArrayOfThingie
{
[XmlAttribute("version")]
public string Version { get; set; }
[XmlElement("Thingie")]
public Thingie[] Thingies { get; set; }
}
static void Main(string[] args)
{
Thingie[] thingies = new[] { new Thingie { Name = "one" }, new Thingie { Name = "two" } };
ArrayOfThingie at = new ArrayOfThingie { Thingies = thingies, Version = "1.0" };
XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfThingie));
StringWriter writer = new StringWriter();
serializer.Serialize(writer, at);
Console.WriteLine(writer.ToString());
}

A bit of a hack would be to serialize the array to XML and then modify the serialized XML before saving. A cleaner way assuming the Array is a property of a class would be to Add an attribute to a serialized XML node.

Related

De-serialize XML files with varying XmlRoot element names

I have a large number of XML files that I need to perform de-serialization on. These files have varying root names (over 250). I'm trying to pass the root attribute name through XmlSerializer before accessing the XML class to retrieve my data. Here is what I have but I'm still getting an error that the root name was expected although the XmlElement class is passing the attribute to the XmlSerializer class.
The method used to retrieve the file:
string strXmlDoc = path;
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(strXmlDoc);
XmlElement objRootElem = objXmlDoc.DocumentElement;
XmlSerializer xmlSerial = new XmlSerializer(typeof(XMLFile), new XmlRootAttribute(objRootElem.ToString()));
StreamReader sr = new StreamReader(path);
XMLFile entity = xmlSerial.Deserialize(sr) as XMLFile;
The XML classes file:
[Serializable]
//[XmlRoot("randomname")] Removed this line since I'm getting the XmlRoot attribute in the XmlSerializer line.
public class XMLFile
{
[System.Xml.Serialization.XmlElement("RECORD")]
public RECORD RECORD { get; set; }
}
[Serializable]
public class RECORD
{
[XmlElement("BK01")]
public Record Bk01 { get; set; }
[XmlElement("BK02")]
public Record Bk02 { get; set; }
}
[Serializable]
public class Record
{
[XmlAttribute("Value")]
public string Value { get; set; }
}
Change this:
XmlSerializer xmlSerial =
new XmlSerializer(typeof(XMLFile), new XmlRootAttribute(objRootElem.ToString()));
to this:
XmlSerializer xmlSerial =
new XmlSerializer(typeof(XMLFile), new XmlRootAttribute(objRootElem.Name));
^^^
XmlElement.ToString() will always return System.Xml.XmlElement, which is not what you want.

Why are arrays being shown as null after serializing using DataContractJsonSerializer?

There have been a number of questions about serializing and deserializing arrays using DataContractJsonSerializer (including one from me: How can I serialise a string array to JSON using DataContractJsonSerializer?) but none seem to answer the current problem that I'm having.
I am converting an XML string to a JSON string by deserializing the XML to a DataContract object and then serializing that object to JSON, using DataContractJsonSerializer. My approach is the same as I have used on a number of other objects, all of which are serializing to JSON perfectly, but I have an object in which an array property is always rendered as null after serialization.
The classes are defined as follows:-
[DataContract]
public class Order
{
[DataMember(Name = "consignments")]
[XmlElement("consignments")]
public Consignment[] Consignments { get; set; }
}
[DataContract]
public class Consignment
{
[DataMember(Name = "conh_id")]
[XmlElement("conh_id")]
public string ConsignmentHeaderId { get; set; }
[DataMember(Name = "conh_status")]
[XmlElement("conh_status")]
public string ConsignmentHeaderStatus { get; set; }
[DataMember(Name = "consignmententries")]
[XmlElement("consignmententries")]
public ConsignmentEntry[] ConsignmentEntries { get; set; }
}
The XML I'm using looks like this:-
<order>
<consignments>
<consignment>
<conh_id>A19708176</conh_id>
<conh_status>ACCEPTED</conh_status>
<consignmententries>
<consignmententry>
<conl_lineNbr>10000</conl_lineNbr>
<conl_sku>SEC01XXZBUXXX</conl_sku>
<conl_original_qty>1</conl_original_qty>
</consignmententry>
</consignmententries>
</consignment>
</consignments>
</order>
The deserializing and serializing is done in the following methods:-
private object DeserialiseXml(string xml)
{
var serialiser = new XmlSerializer(typeof(Order));
var stringReader = new StringReader(xml);
var result = serialiser.Deserialize(stringReader);
return result;
}
private string SerialiseJson(object serialisable)
{
using (MemoryStream stream = new MemoryStream())
{
var serialiser = new DataContractJsonSerializer(serialisable.GetType());
serialiser.WriteObject(stream, serialisable);
var json = Encoding.UTF8.GetString(stream.ToArray());
return json;
}
}
When I test it, the Consignments property is always null in the resulting JSON.
"order": {
"consignments": [
{
"conh_id": null,
"conh_status": null,
"consignmententries": null
}
]
}
Debugging shows that this property is null in the object created after the deserialization step so the problem is in the XML deserialization rather than the JSON serialization.
What do I need to change on my object model to get the array converted properly?
Your problem is happening when you deserialize from XML rather than when you serialize to JSON. In your XML, the collections have been serialized with two levels: an outer container element and an element for each item:
<consignments>
<consignment>
<!-- Consignment data -->
</consignment>
</consignments>
And
<consignmententries>
<consignmententry>
<!-- Entry data -->
</consignmententry>
</consignmententries>
(For comparison, in the linked question the Labels XML collection has a single level). Thus you need to use [XmlArray(string name)] and [XmlArrayItem(string itemName)] to specify the outer and inner element names:
[DataContract]
[XmlRoot("order")]
public class Order
{
[DataMember(Name = "consignments")]
[XmlArray("consignments")]
[XmlArrayItem("consignment")]
public Consignment[] Consignments { get; set; }
}
[DataContract]
public class Consignment
{
[DataMember(Name = "conh_id")]
[XmlElement("conh_id")]
public string ConsignmentHeaderId { get; set; }
[DataMember(Name = "conh_status")]
[XmlElement("conh_status")]
public string ConsignmentHeaderStatus { get; set; }
[DataMember(Name = "consignmententries")]
[XmlArray("consignmententries")]
[XmlArrayItem("consignmententry")]
public ConsignmentEntry[] ConsignmentEntries { get; set; }
}
Conversely, adding the attribute [XmlElement] to a collection tells XmlSerializer that the collection should be serialized without the outer container element.
You also need to add [XmlRoot("order")] to Order to specify the root element name.
Now your XML will deserialize successfully.

Trouble deserializing XML into a List<T>

Here's the XML file I'm trying to deserialize:
<?xml version="1.0" encoding="utf-8"?>
<EntityType>
<Name>SomeName</Name>
<Components>
<ComponentAssembly>Some assembly name</ComponentAssembly>
</Components>
</EntityType>
And here is the Data contract I am using to deserialize it:
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace GameUtilities.Entities.DataContracts
{
[DataContract(Name="EntityType",Namespace="")]
public class EntityTypeData
{
[DataMember(IsRequired=true,Order = 0)]
public string Name { get; private set; }
[DataMember(IsRequired=false,Order=1)]
public List<ComponentEntry> Components { get; private set; }
public EntityTypeData(string name, List<ComponentEntry> components = null)
{
Name = name;
if(components == null)
{
Components = new List<ComponentEntry>();
}
else
{
Components = components;
}
}
}
[DataContract]
public class ComponentEntry
{
[DataMember(IsRequired = true, Order = 0)]
public string ComponentAssembly { get; private set; }
public ComponentEntry(string componentAssembly)
{
ComponentAssembly = componentAssembly;
}
}
}
Deserializing it works correctly, but the Components list is always empty, no matter how many entrys I put inside the tags. I have tried marking the [DataMemeber] attribute for Components as "IsRequired=true", and deserialization still completes without error, but the List is not getting populated. Can you see any issues with my data contract that would make this fail?
EDIT: As a test, I ran an object using the Data Contract above through a serializer to see what XML got spat out. Here's what I saw:
<EntityType xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Name>TESTNAME</Name>
<Components xmlns:a="http://schemas.datacontract.org/2004/07/GameUtilities.Entities.DataContracts">
<a:ComponentEntry>
<a:ComponentAssembly>ONE</a:ComponentAssembly>
</a:ComponentEntry>
<a:ComponentEntry>
<a:ComponentAssembly>TWO</a:ComponentAssembly>
</a:ComponentEntry>
<a:ComponentEntry>
<a:ComponentAssembly>THREE</a:ComponentAssembly>
</a:ComponentEntry>
</Components>
Here is the serialization code I used:
public static void SerializeObject<T>(string path, T obj)
{
FileStream fs = new FileStream(path,FileMode.Create);
XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
DataContractSerializer ser = new DataContractSerializer(typeof(T));
//Serialize the data to a file
ser.WriteObject(writer, obj);
writer.Close();
fs.Close();
}
As you can see, there is a separate ComponentEntry being created for each ComponentAssembly that is listed. Is there a way to get rid of that and just get the ComponentAssembly?
Easiest solution would be to define a collection data contract instead. I don't think there is a way to have the ComponentEntry as a separate type, when using DataContractSerializer.
[DataContract(Name="EntityType",Namespace="")]
public class EntityTypeData
{
[DataMember(IsRequired=true,Order=0)]
public string Name { get; private set; }
[DataMember(IsRequired=false,Order=1)]
public ComponentList Components { get; private set; }
public EntityTypeData(string name, IEnumerable<string> components = null)
{
Name = name;
Components = new ComponentList();
if(components != null)
{
Components.AddRange(components);
}
}
}
[CollectionDataContract(ItemName = "ComponentAssembly", Namespace="")]
public class ComponentList : List<string> {}
var ser = new DataContractSerializer(typeof(EntityTypeData));
var entity = (EntityTypeData) ser.ReadObject(stream);

C# Deserialze XML to an Object

I am trying to deserialize xml to an object using C#.
This is my XML.
<Products>
<Product>
<Name>Test</Name>
<Price Amount="12.95">£ 12.95</Price>
</Product>
</Products>
And this is my code.
class Program
{
static void Main(string[] args)
{
var filePath = #"C:\Eastpoint\TestApps\TestHunterSuppliers\bin\Debug\Sample.xml";
var reader = new XmlTextReader(filePath);
var serializer = new XmlSerializer(typeof(Products));
var products = (Products)serializer.Deserialize(reader);
Console.WriteLine(products.Product.Name);
Console.WriteLine(products.Product.Price.Amount);
}
}
public class Products
{
public Product Product { get; set; }
}
public class Product
{
public string Name { get; set; }
public Price Price { get; set; }
}
public class Price
{
public string Amount { get; set; }
public string Value { get; set; }
}
By using the above code I am getting the product object but properties of the price object are always deserializing to null values.
Could somebody advice me what I am missing.
Thanks,
Naresh
The default behavior of .NET's XML serializer is to serialize properties to XML elements. The value of the property becomes the inner text of the XML element corresponding to the property. That is, if you serialized your objects it would look like this:
<Products>
<Product>
<Name>Test</Name>
<Price>
<Amount>12.95</Amount>
<Value>£ 12.95</Value>
</Price>
</Product>
</Products>
In your case, you need to instruct the serializer to put Price.Amount into an attribute and to write Price.Value as Price's inner text. The easiest way to do this is to decorate the properties requiring non-default serialization with appropriate [XmlXxx] attributes:
...
[XmlAttribute]
public string Amount { get ; set ; }
[XmlText]
public string Value { get ; set ; }
...
Incidentally, if your Products is supposed to contain more than one product, you will need to modify your code like this:
...
[XmlElement ("Product")]
public Product[] All { get ; set ; }
...
The attribute instructs the serializer not to create an <All> element to hold the individual products' elements. You can also use other collections like List<Product>, but you should create them beforehand like this:
...
[XmlElement ("Product")]
public readonly List<Product> All = new List<Product> () ;
...
You can make your original test code working using FileStream,
Tested code sample:
var filePath = #"C:\Eastpoint\TestApps\TestHunterSuppliers\bin\Debug\Sample.xml";
var serializer = new XmlSerializer(typeof(Products));
//Setting dummy object to create the xml
Products myProducts = new Products { Product = new Product { Name = "Papers", Price = new Price { Amount = "10", Value = "20" } } };
var strWrite = new FileStream(filePath, FileMode.Create);
serializer.Serialize(strWrite, myProducts);
strWrite.Close();
//////////////////////////////
var strRead = new FileStream(filePath, FileMode.Open);
var products = (Products)serializer.Deserialize(strRead);
strRead.Close();
Console.WriteLine(products.Product.Name);
Console.WriteLine(products.Product.Price.Amount);
And to get the price as an attribute in the XML document:
public class Price
{
[XmlAttribute]
public string Amount { get; set; }
public string Value { get; set; }
}

c# XML serialization

I'd like to serialize something like this, where there is a header and a body.
The first part "galleryData" is the header
The 2nd part is "imageData" - repeated for each image in gallery
<galleryData>
<title>some title</title>
<uuid>32432322</uuid>
<imagepath>some path</imagepath>
</galleryData>
<imageData>
<title>title one</title>
<category>nature</category>
<description>blah blah</description>
</imageData>
<imageData>
<title>title two</title>
<category>nature</category>
<description>blah blah</description>
</imageData>
<imageData>
<title>title three</title>
<category>nature</category>
<description>blah blah</description>
</imageData>
I see how to do it if I didn't need a header area. I'm currently just using xmlwriter to create it, but I'd like to serialize the object out to xml instead.
You need a root in order to have valid XML. Here's an example of how your model might look like:
public class ImageData
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("category")]
public string Category { get; set; }
[XmlElement("description")]
public string Description { get; set; }
}
public class GalleryData
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("uuid")]
public string UUID { get; set; }
[XmlElement("imagepath")]
public string ImagePath { get; set; }
}
public class MyData
{
[XmlElement("galleryData")]
public GalleryData GalleryData { get; set; }
[XmlElement("imageData")]
public ImageData[] ImageDatas { get; set; }
}
and then simply create an instance of this model and serialize it to a stream:
class Program
{
static void Main()
{
var myData = new MyData
{
GalleryData = new GalleryData
{
Title = "some title",
UUID = "32432322",
ImagePath = "some path"
},
ImageDatas = new[]
{
new ImageData
{
Title = "title one",
Category = "nature",
Description = "blah blah"
},
new ImageData
{
Title = "title two",
Category = "nature",
Description = "blah blah"
},
}
};
var serializer = new XmlSerializer(myData.GetType());
serializer.Serialize(Console.Out, myData);
}
}
Given the way that XML serialization works, I do not believe the structure you are looking for will be possible from a straight Object -> XML structure as in your example you have more than one root node.
If you had something where there was a container node, then individual ImageData elements within them, or a single over arching element to bundle them together you might be able to get by with it.
If you are using XmlSerialization, you will need a root element - which will be the class that you serialize.
Your galleryData and imageData will be object instance variables within the class used for your root element.
You can create an object for header with properties an mark the with XmlSerializabe and the add a field of type List so that standart xml serialized will serialize it as child elements of header
You can use something like the following to serialize a .NET object to an XML string:
protected string ObjectToXml<T>(T obj)
{
var sw = new StringWriter();
try
{
var mySerializer = new XmlSerializer(typeof(T));
mySerializer.Serialize(sw, obj);
}
catch (Exception ex)
{
// Error logging here
}
return sw.ToString();
}
You will need a root element for your XML document, and if your current model doesn't match the desired serialized form, then create and populate an appropriate data transfer object to do the serialisation job.
The structure you're showing is not valid XML because is containing more then one root node, so you can forget about XmlSerializer. If you want to handle easily with such a xml similar structures I suggest Html Agility Pack

Categories

Resources