Including arrary index in XML Serialization - c#

I have a class that looks like this
public class SomeClass
{
public SomeChildClass[] childArray;
}
which will output XML from the XMLSerializer like this:
<SomeClass>
<SomeChildClass>
...
</SomeChildClass>
<SomeChildClass>
...
</SomeChildClass>
</SomeClass>
But I want the XML to look like this:
<SomeClass>
<SomeChildClass index=1>
...
</SomeChildClass>
<SomeChildClass index=2>
...
</SomeChildClass>
</SomeClass>
Where the index attribute is equal to the items position in the array.
I could add an index property to SomeChildClass with the "XMLAttribute" attribute but then I would have to remember to loop through the array and set that value before I serialize my object.
Is there some attribute i can add or some other way to automatically generate the index attribute for me?

The best approach would be to do what you said and add a property to the "SomeChildClass" like this
[XmlAttribute("Index")]
public int Order
{ { get; set; } }
Then however you are adding these items to your array, make sure that this property get's set. Then when you serialize....Presto!

You may need to look into implementing System.Xml.Serialization.IXmlSerializable to accomplish this.

You can check XmlAttributeOverrides Class.

Related

XmlSerializer: serialize array of elements as strings with different element names

My array items can have different names, but they all have simple string values, e.g.:
<MyArray>
<TypeA>foo</TypeA>
<TypeA>bar</TypeA>
<TypeB>bazz</TypeB>
</MyArray>
How do I achieve this?
I'm looking at the documentation on MSDN here: https://msdn.microsoft.com/en-us/library/2baksw0z(v=vs.110).aspx
There is an example that looks like what I want, but I can't get it to work the way they say it should:
public class Employee {
public string Name;
}
public class Group {
[XmlArrayItem("MemberName")]
public Employee[] Employees;
}
The resulting XML will supposedly look like this:
<Group>
<Employees>
<MemberName>Haley</MemberName>
</Employees>
</Group>
However, when I run this example, I get the following XML instead:
<Group>
<Employees>
<MemberName>
<Name>Haley</Name>
</MemberName>
</Employees>
</Group>
I'm assuming there's a mistake in the documentation (I don't see anything in their code that should magically result in the value of the class Employee being substituted by the value its Name property), but I'm actually interested in getting my XML to look like their (erroneous?) example.
I found the solution as soon as I posted the question: use XmlTextAttribute.
In their example, Employee class should have looked like this:
public class Employee {
[XmlText]
public string Name;
}
In my case, my collection can contain TypeA and TypeB, where each type has one member with an [XmlText] attribute.

Deserialize xml attribute to List<String>

I am trying to deserialize an attribute with a list of pages to a List<String> object. If I use a space delimiter, I am able to deserialize it just fine, but I want it to look cleaner with a comma or a vertical bar.
Is it possible to deserialize
<Node Pages="1,2">
into a List<String> or List<int>?
My class is simple as well.
public List<int> Pages;
XmlSerializer does not support this out of the box for attributes. If you are in control of the structure of the XML, consider using child elements because using character separated attribute strings kind of beats the purpose of XML altogether.
You could work around it though using a computed property. But bear in mind that this will be slower because every time you access this property, you will parse the comma-separated string.
[XmlAttribute(AttributeName = "Pages")]
public string PagesString { get; set; }
public IList<int> Pages {
get {
return PagesString.Split(',').Select(x => Convert.ToInt32(x.Trim())).ToList();
}
set {
PagesString = String.Join(",", value);
}
}
Also, this silly implementation does not take into account that there might be wrong input in your string. You should guard against that too.
Another thing to notice is that every time you access the Pages property, a new collection is returned. If you invoke Add() on the returned value, this change will not be reflected in your XML.

Programmatically set properties to exclude from serialization

Is it possible to programmatically set that you want to exclude a property from serialization?
Example:
When de-serializing, I want to load up an ID field
When serializing, I want to NOT output the ID field
I believe there are three options here:
Use XmlIgnore attribute. The downside is that you need to know in advance which properties you want the xmlserializer to ignore.
Implement the IXmlSerializable interface. This gives you complete control on the output of XML, but you need to implement the read/write methods yourself.
Implement the ICustomTypeDescriptor interface. I believe this will make your solution to work no matter what type of serialization you choose, but it is probably the lengthiest solution of all.
It depends on serialization type. Here full example for doing this with BinaryFormatter:
You may use OnDeserializedAttribute:
[Serializable]
class SerializableEntity
{
[OnDeserialized]
private void OnDeserialized()
{
id = RetrieveId();
}
private int RetrievId() {}
[NonSerialized]
private int id;
}
And there is another way to do this using IDeserializationCallback:
[Serializable]
class SerializableEntity: IDeserializationCallback
{
void IDeserializationCallback.OnDeserialization(Object sender)
{
id = RetrieveId();
}
private int RetrievId() {}
[NonSerialized]
private int id;
}
Also you may read great Jeffrey Richter's article about serialization: part 1 and part 2.
If you are serializing to XML, you can use XMLIgnore
As in:
class SomeClass
{
[XmlIgnore] int someID;
public string someString;
}
An old post, but I found ShouldSerialize pattern
http://msdn.microsoft.com/en-us/library/53b8022e%28VS.71%29.aspx That is really HELPFUL!!!
If you want to include field during serialization but ignore it during deserialization then you can use OnDeserializedAttribute to run a method which will set default value for ID field.
If you're using XML serialization, use the [XmlIgnore] attribute. Otherwise, how to ignore a particular property is defined by the serializer itself.
The NonSerializedAttribute attribute.
http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx

Ignoring a property during deserialization

I have a class that is serializing very nicely - finally!
Now I want to add a property to this class, that I don't want to be serialized at all.
Is it possible to add this new property with some kind of attribute so that when I call serialize or deserialize methods, this property will go unnoticed?
[XmlIgnore]
public int DoNotSerialize { get { ... } set { ... } }
I think your looking for [XmlIgnore] attribute

XmlSerialize a custom collection with an Attribute

I've got a simple class that inherits from Collection and adds a couple of properties. I need to serialize this class to XML, but the XMLSerializer ignores my additional properties.
I assume this is because of the special treatment that XMLSerializer gives ICollection and IEnumerable objects. What's the best way around this?
Here's some sample code:
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
namespace SerialiseCollection
{
class Program
{
static void Main(string[] args)
{
var c = new MyCollection();
c.Add("Hello");
c.Add("Goodbye");
var serializer = new XmlSerializer(typeof(MyCollection));
using (var writer = new StreamWriter("test.xml"))
serializer.Serialize(writer, c);
}
}
[XmlRoot("MyCollection")]
public class MyCollection : Collection<string>
{
[XmlAttribute()]
public string MyAttribute { get; set; }
public MyCollection()
{
this.MyAttribute = "SerializeThis";
}
}
}
This outputs the following XML (note MyAttribute is missing in the MyCollection element):
<?xml version="1.0" encoding="utf-8"?>
<MyCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Hello</string>
<string>Goodbye</string>
</MyCollection>
What I want is
<MyCollection MyAttribute="SerializeThis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Hello</string>
<string>Goodbye</string>
</MyCollection>
Any ideas? The simpler the better. Thanks.
Collections generally don't make good places for extra properties. Both during serialization and in data-binding, they will be ignored if the item looks like a collection (IList, IEnumerable, etc - depending on the scenario).
If it was me, I would encapsulate the collection - i.e.
[Serializable]
public class MyCollectionWrapper {
[XmlAttribute]
public string SomeProp {get;set;} // custom props etc
[XmlAttribute]
public int SomeOtherProp {get;set;} // custom props etc
public Collection<string> Items {get;set;} // the items
}
The other option is to implement IXmlSerializable (quite a lot of work), but that still won't work for data-binding etc. Basically, this isn't the expected usage.
If you do encapsulate, as Marc Gravell suggests, the beginning of this post explains how to get your XML to look exactly like you describe.
http://blogs.msdn.com/youssefm/archive/2009/06/12/customizing-the-xml-for-collections-with-xmlserializer-and-datacontractserializer.aspx
That is, instead of this:
<MyCollection MyAttribute="SerializeThis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<string>Hello</string>
<string>Goodbye</string>
<Items>
</MyCollection>
You can have this:
<MyCollection MyAttribute="SerializeThis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Hello</string>
<string>Goodbye</string>
</MyCollection>
As Neil Whitaker suggests and in case his link dies..
Create an inner collection to store the strings and apply the XmlElement attribute to mask the collection name. Produces the same xml output as if MyCollection inherited from Collection, but also serializes attributes on the parent element.
[XmlRoot("MyCollection")]
public class MyCollection
{
[XmlAttribute()]
public string MyAttribute { get; set; }
[XmlElement("string")]
public Collection<string> unserializedCollectionName { get; set; }
public MyCollection()
{
this.MyAttribute = "SerializeThis";
this.unserializedCollectionName = new Collection<string>();
this.unserializedCollectionName.Add("Hello");
this.unserializedCollectionName.Add("Goodbye");
}
}
I've been fighting with the same issue Romaroo is (wanting to add properties to the xml serialization of a class that implements ICollection). I have not found any way to expose properties that are in the collection class. I even tried using the XmlAttribute tag and making my properties show up as attributes of the root node, but no luck there either. I was however able to use the XmlRoot tag on my class to rename it from "ArrayOf...". Here are some references in case you're interested:
MilkCarton.com
Microsoft Forum Posting
Diranieh - look halfway down
Sometimes you just want to do what you just want to do; the Framework be damned.
I posted an answer here Property of a List<T> Not Deserialized
that does what the OP wanted to do.

Categories

Resources