How to generate an XML using XMLSerializer? [duplicate] - c#

This question already has answers here:
Serialize an object to XML
(19 answers)
Closed 5 years ago.
I would like to generate an XML using XMLSerializer. I have an abstract Base class which is being inherited by other classes.
public abstract class Base
{
public string Name {get; set;}
public int ID {get; set;}
public Base(string Name, int ID)
{
this.Name = Name;
this.ID = ID;
}
}
public class HR: Base
{
public HR(string Name, int ID): Base(Name,ID)
{
}
}
public class IT : Base
{
public IT(string Name, int ID): Base(Name,ID)
{
}
}
I am not sure how to generate an XML of format
<Employee>
<HR>
<Name> </Name>
<ID> </ID>
</HR>
<IT>
<Name> </Name>
<ID> </ID>
</IT>
</Employee>
I apologise for the vague question. I have never used XMLSerializer before and not sure how to proceed with it. Any help would be greatly appreciated.
Thanks

As I read your xml, it seems you want to serialize a list of Employee.
I have a solution for you if your list is a member of a class (not directly serializing the list).
public abstract class Employee
{
public string Name { get; set; }
public int ID { get; set; }
public Employee(string Name, int ID)
{
this.Name = Name;
this.ID = ID;
}
}
public class HR : Employee
{
public HR() : base(null, 0) { } // default constructor is needed for serialization/deserialization
public HR(string Name, int ID) : base(Name, ID) { }
}
public class IT : Employee
{
public IT() : base(null, 0) { }
public IT(string Name, int ID) : base(Name, ID) { }
}
public class Group
{
[XmlArray("Employee")]
[XmlArrayItem("HR",typeof(HR))]
[XmlArrayItem("IT",typeof(IT))]
public List<Employee> list { get; set; }
public Group()
{
list = new List<Employee>();
}
}
class Program
{
static void Main(string[] args)
{
Group grp = new Group();
grp.list.Add(new HR("Name HR", 1));
grp.list.Add(new IT("Name IT", 2));
XmlSerializer ser = new XmlSerializer(typeof(Group));
ser.Serialize(Console.Out, grp);
}
}
And the output is :
<?xml version="1.0" encoding="ibm850"?>
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Employee>
<HR>
<Name>Name HR</Name>
<ID>1</ID>
</HR>
<IT>
<Name>Name IT</Name>
<ID>2</ID>
</IT>
</Employee>
</Group>
Very similar to your desired output, excepted one more element at the root "Group".
Deserialize with the same XmlSerializer(typeof(Group)) should work as well.

I think you need to use the XmlType attributes to make sure your elements show up as <HR> and <IT> instead of <employee xsi:type="HR">. Working demo below:
public abstract class Employee
{
public string Name { get; set; }
public string ID { get; set; }
public Employee(string Name, string ID)
{
this.Name = Name;
this.ID = ID;
}
}
public class HR : Employee
{
public HR(string Name, string ID) : base(Name, ID)
{
}
public HR() : base("No name", "No ID")
{
}
}
public class IT : Employee
{
public IT(string Name, string ID) : base(Name, ID)
{
}
public IT() : base("No name", "No ID")
{
}
}
I added default (parameter-less) constructors for the serializer.
Then you have to have some kind of wrapper object to handle a list of Employees:
public class Employees
{
[XmlElement(typeof(IT))]
[XmlElement(typeof(HR))]
public List<Employee> Employee { get; set; } //It doesn't really matter what this field is named, it takes the class name in the serialization
}
Next, you can use the serializer code from my comment to generate the XML:
var employees = new Employees
{
Employee = new List<Employee>()
{
new IT("Sugan", "88"),
new HR("Niels", "41")
}
};
var serializer = new XmlSerializer(typeof(Employees));
var xml = "";
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw))
{
serializer.Serialize(writer, employees);
xml = sw.ToString();
}
}
Console.WriteLine(xml);
(Namespaces ommitted for clarity's sake)
This returns the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<IT>
<Name>Sugan</Name>
<ID>88</ID>
</IT>
<HR>
<Name>Niels</Name>
<ID>41</ID>
</HR>
</Employees>

Add the [Serializable] Annotation to the class you want to serialize.
[System.Serializable]
public class Base
{
public string Name { get; set; }
public int ID { get; set; }
public Base(string Name, int ID)
{
this.Name = Name;
this.ID = ID;
}
}
To serialize in XML format, use the following code:
System.Xml.Serialization.XmlSerializer Serializer = new System.Xml.Serialization.XmlSerializer(typeof(Base));
Base Foo = new Base();
string xmldata = "";
using (var stringwriter = new System.IO.StringWriter())
{
using (System.Xml.XmlWriter xmlwriter = System.Xml.XmlWriter.Create(stringwriter))
{
Serializer.Serialize(xmlwriter, Foo);
xml = stringwriter.ToString(); // Your XML
}
}
To deserialize from XML back to your Base object, use the following code:
System.IO.MemoryStream FooStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml));
Base Foo;
Foo = (Base)Serializer.Deserialize(FooStream);

Related

Incorrect XML structure c# [duplicate]

I am having an issue with serializing and object, I can get it to create all the correct outputs except for where i have an Element that needs a value and an attribute. Here is the required output:
<Root>
<Method>Retrieve</Method>
<Options>
<Filter>
<Times>
<TimeFrom>2009-06-17</TimeFrom>
</Times>
<Document type="word">document name</Document>
</Filter>
</Options>
</AdCourierAPI>
I can build all of it but can not find a way to set the Document type attribute, here is a segment of the object class
[XmlRoot("Root"), Serializable]
public class Root
{
[XmlElement("Method")]
public string method="RetrieveApplications";
[XmlElement("Options")]
public _Options Options;
}
public class _Options
{
[XmlElement("Filter")]
public _Filter Filter;
}
public class _Filter
{
[XmlElement("Times")]
public _Times Times;
[XmlElement("Documents")]
public string Documents;
}
which gives me:
<Document>document name</Document>
rather than:
<Document type="word">document name</Document>
but I can not find a way to correct this, please advise.
Thanks
Where do you have the type stored?
Normally you could have something like:
class Document {
[XmlAttribute("type")]
public string Type { get; set; }
[XmlText]
public string Name { get; set; }
}
public class _Filter
{
[XmlElement("Times")]
public _Times Times;
[XmlElement("Document")]
public Document Document;
}
The string class doesn't have a type property, so you can't use it to create the desired output. You should create a Document class instead :
public class Document
{
[XmlText]
public string Name;
[XmlAttribute("type")]
public string Type;
}
And you should change the Document property to type Document
It sounds like you need an extra class:
public class Document
{
[XmlAttribute("type")]
public string Type { get; set; }
[XmlText]
public string Name { get; set; }
}
Where an instance (in the example) would have Type = "word" and Name = "document name"; documents would be a List<Document>.
By the way - public fields are rarely a good idea...
You can use XmlWriter instead XmlSerialization to get this effect.
It is more complex but if you have a lot of strings in model it will be cleaner solution.
Create your own CustomeAttribute, for example:
[System.AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class MyCustomAttribute : System.Attribute
{
public MyCustomAttribute (string type)
{
MyType = type;
}
public string MyType { get; set; }
}
Then in model add it, like that:
public class MyModel
{
[MyCustom("word")]
public string Document { get; set; }
[MyCustom("time")]
public string Time { get; set; }
}
The last part is to create xml with this arguments.
You can do it likes that:
var doc = new XmlDocument();
MyModel myModel = new MyModel();//or get it from somewhere else
using (Stream s = new MemoryStream())
{
var settings = new XmlWriterSettings();
settings.Async = true;
settings.Indent = true;
var writer = XmlTextWriter.Create(s, settings);
await writer.WriteStartDocumentAsync();
await writer.WriteStartElementAsync(null,"Root", null);
myModel.GetType().GetProperties().ToList().ForEach(async p =>
{
dynamic value = p.GetValue(myModel);
writer.WriteStartElement(p.Name);
var myCustomAttribute = p.GetCustomAttributes(typeof(MyCustomAttribute), false).FirstOrDefault() as MyCustomAttribute;
if(myCustomAttribute != null)
{
await writer.WriteAttributeStringAsync(null, "MyType", null, myCustomAttribute.MyType );
}
writer.WriteValue(value);
await writer.WriteEndElementAsync();
});
await writer.WriteEndElementAsync();
await writer.FlushAsync();
s.Position = 0;
doc.Load(s);
writer.Close();
}
string myXml = doc.OuterXml
In myXml should be something like that:
(values are examples)
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Document MyType="word">something</Document>
<Time MyType="time">11:31:29</Time>
</Root>
You can do it in other way, of course.
Here you have some docs which helped me:
https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlwriter?view=netframework-4.8#writing_elements

Serializing class implementing ICollection

I have a collection class implementing ICollection<T> with a few custom attributes thrown in for completeness..
In this simplistic sample, its a simple Request/Results pattern with the request itself being passed back as an attribute of the results class.
[Serializable]
public class MyRequest
{
public int SearchID { get; set; }
}
[Serializable]
public class MyResults : ICollection<MyElement>
{
public MyRequest RequestDetails { get; set; }
private ICollection<MyElement> _list = new List<MyElement>();
/* ICollection interface methods removed */
}
[Serializable]
public class MyElement
{
public int ID { get; set; }
}
Here's the sample program to instantiate and then output a serialized string.
class Program
{
static void Main(string[] args)
{
MyResults m = new MyResults();
m.RequestDetails = new MyRequest() { SearchID = 1 };
for (int i = 1; i <= 5; i++)
{
m.Add(new MyElement { ID = i });
}
XmlDocument xmlDoc = new XmlDocument();
XmlSerializer xmlSerializer = new XmlSerializer(m.GetType());
using (MemoryStream xmlStream = new MemoryStream())
{
xmlSerializer.Serialize(xmlStream, m);
xmlStream.Position = 0;
xmlDoc.Load(xmlStream);
}
System.Diagnostics.Debug.WriteLine(xmlDoc.OuterXml);
}
}
The problem is that the output is not including the MyRequest details...
<?xml version="1.0"?>
<ArrayOfMyElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyElement>
<ID>1</ID>
</MyElement>
<MyElement>
<ID>2</ID>
</MyElement>
<MyElement>
<ID>3</ID>
</MyElement>
<MyElement>
<ID>4</ID>
</MyElement>
<MyElement>
<ID>5</ID>
</MyElement>
</ArrayOfMyElement>
XmlSerializer always ignores extra properties when serializing a collection; the only way to do it, as far as I know, is not to implement ICollection<MyElement> on your MyResults class, and instead expose the collection as a property:
public class MyResults
{
public MyRequest RequestDetails { get; set; }
public ICollection<MyElement> Items { get; set; }
}
(the Serializable attribute isn't needed for XML serialization)
Just change ICollection to Collection because XmlSerialization does not support Generic Interfaces:
public class MyResults
{
public MyResults()
{
this.Items= new Collection<MyElement>();
}
public MyRequest RequestDetails { get; set; }
public Collection<MyElement> Items { get; set; }
}

How to serialize container classes using custom serializer in C#

I need to serialize some container classes for which the HasValue property is evaluated to true.
I don´t need to remove invalid elements from the container List prior to the serialization. The serializer should be able to determine which objects need to be serialized or not. I guess a custom serializer can be suitable for my need but i don´t know how to figure out this. Any other solution / best practice would be appreciated.
Here my classes
public static class ContainerFactory
{
public static Container Create()
{
var container = new Container();
container.Persons.AddRange(new[]
{
new Person
{
FirstName = "Thomas"
},
new Person
{
FirstName = "Andrew",
LastName = "Martin",
Vehicles = new Vehicles
{
new Vehicle { Hsn = "65976GHR", Tsn = "HUZUKL"}
}
},
new Person
{
FirstName = "Arnold",
LastName = "Beckmann",
Vehicles = new Vehicles
{
new Vehicle { Hsn = "345XXXHZ"},
new Vehicle { Hsn = "659JUKI", Tsn = "787999HGF"}
}
}
});
return container;
}
}
[Serializable]
public class Container
{
public Container()
{
Persons = new Persons();
}
public Persons Persons { get; set; }
public void Serialize()
{
var serializer = new XmlSerializer(typeof (Container));
var streamWriter = new StreamWriter(#"C:\container.xml", false);
serializer.Serialize(streamWriter, this);
}
}
public class Persons: List<Person>
{
}
public class Vehicles: List<Vehicle>
{
public Vehicles()
{
}
public Vehicles(IEnumerable<Vehicle> vehicles):base(vehicles)
{
}
}
[Serializable]
public class Person : IHasValue
{
public Person()
{
this.Vehicles = new Vehicles();
this.Id = Guid.NewGuid().ToString();
}
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Vehicles Vehicles { get; set; }
public bool HasValue
{
get { return !string.IsNullOrEmpty(this.FirstName) && !string.IsNullOrEmpty(this.LastName); }
}
}
public interface IHasValue
{
bool HasValue { get;}
}
public class Vehicle: IHasValue
{
public string Hsn { get; set; }
public string Tsn { get; set; }
public bool HasValue
{
get { return !string.IsNullOrEmpty(Hsn) && !string.IsNullOrEmpty(Tsn); }
}
}
//Using the .NET XMLSerializer to test my container
Container container = ContainerFactory.Create();
container.Serialize();
Console.WriteLine("Press any Key to continue...");
Console.ReadLine();
Output
<?xml version="1.0" encoding="utf-8"?>
<Container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Persons>
<Person>
<Id>76cdcc18-b256-40fe-b813-cd6c60e682ca</Id>
<FirstName>Thomas</FirstName>
<Vehicles />
</Person>
<Person>
<Id>26623bf9-d799-44d2-bc1a-7ec91292d1cd</Id>
<FirstName>Andrew</FirstName>
<LastName>Martin</LastName>
<Vehicles>
<Vehicle>
<Hsn>65976GHR</Hsn>
<Tsn>HUZUKL</Tsn>
</Vehicle>
</Vehicles>
</Person>
<Person>
<Id>f645cde1-10c8-4df5-81df-9b9db7712ec3</Id>
<FirstName>Arnold</FirstName>
<LastName>Beckmann</LastName>
<Vehicles>
<Vehicle>
<Hsn>345XXXHZ</Hsn>
</Vehicle>
<Vehicle>
<Hsn>659JUKI</Hsn>
<Tsn>787999HGF</Tsn>
</Vehicle>
</Vehicles>
</Person>
</Persons>
How can I achieve my goal to serialize Vehicles/Persons only for which HasValue == true?
you need to use:
ISerializable interface - every class that you want to serialize and every property you want in different node need to use the appropriate attribute. look at the example here
when you serialize a class check if HasValue == true
Persons : List < Person > should be also Serializable.
public void GetObjectData( SerializationInfo info, StreamingContext context )
{
foreach (Person person in this)
{
if(person.HasValue)
{
info.AddValue("Firsname", person, typeof(Person));
info.AddValue (....);
..............
}
}
}

Generic class to Select element values in Xml to properties of a class

I have an xml like:
<?xml version="1.0" encoding="utf-8" ?>
<Property>
<Owner>
<FirstName>LeBron</FirstName>
<LastName>James</LastName>
</Owner>
<Seller>
<Name>LeBron</Name>
<Code>Seller01</Code>
<Branch>demoBranch</Branch>
</Seller>
<Equipments>
<Equipment>
<Name>Kodle</Name>
<CountryOfOrigin>Bryant</CountryOfOrigin>
</Equipment>
<Equipment>
<Name>Desktop</Name>
<CountryOfOrigin>Kryon</CountryOfOrigin>
</Equipment>
</Equipments>
</Property>
and classes:
public Property()
{
public Owner owner{get;set;}
public Seller seller{get;set;}
public equipment equipment{get;set;}
}
public Owner
{
public FirstName{get;set;}
public LastName{get;set;}
}
public Seller
{
public Name{get;set;}
public Code{get;set;}
public Branch{get;set;}
}
public Equipment
{
public Name{get;set;}
public CountryOfOrigin{get;set;}
}
Of course, we can load the class from the xml one by one,like:
XDocument xDoc = XDocument.Load(xmlFile);
var owner = from e in doc.Descendants("Owner")
select new Owner
{
FirstName = e.Element("FirstName").Value,
LastName = e.Element("LastName").Value
};
or deserialize the xml and get the sub class.
public Property LoadFromDocument(string fileUrl){
Property serializableObject = null;
using(TextReader textReader = textReader = new StreamReader(fileUrl)){
XmlSerializer xmlSerializer = new XmlSerializer(ObjectType);
serializableObject = xmlSerializer.Deserialize(textReader) as Property;
}
return serializableObject;
}
and then get owner by
Owner owner = LoadFromDocument(filrUrl).Owner;
but I want to create a generic class to do like bellow.
public class OXmlRepository<T>
{
public XDocument xDoc { get; set; }
protected XName ElementName { get; private set; }
protected OXmlRepository()
{
ElementName = typeof(T).Name.ToString();
}
public T GetObject()
{
//how can I get the class T instance with the values from Xml
}
}
public T GetObject()
{
var xs = new XmlSerializer(typeof(T));
using(var reader = xDoc.CreateReader())
{
return (T)xs.Deserialize(reader);
}
}

C# XML Serializer won't store an attribute

This is my first question on Stack Overflow. Apologies in advance if I don't do things quite right while I'm learning how things work here.
Here is my code :
public void TestSerialize()
{
ShoppingBag _shoppingBag = new ShoppingBag();
Fruits _fruits = new Fruits();
_fruits.testAttribute = "foo";
Fruit[] fruit = new Fruit[2];
fruit[0] = new Fruit("pineapple");
fruit[1]= new Fruit("kiwi");
_fruits.AddRange(fruit);
_shoppingBag.Items = _fruits;
Serialize<ShoppingBag>(_shoppingBag, #"C:\temp\shopping.xml");
}
public static void Serialize<T>(T objectToSerialize, string filePath) where T : class
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StreamWriter writer = new StreamWriter(filePath))
{
serializer.Serialize(writer, objectToSerialize);
}
}
[Serializable]
public class ShoppingBag
{
private Fruits _items;
public Fruits Items
{
get { return _items; }
set {_items = value; }
}
}
public class Fruits : List<Fruit>
{
public string testAttribute { get; set; }
}
[Serializable]
public class Fruit
{
public Fruit() { }
public Fruit(string value)
{
Name = value;
}
[XmlAttribute("name")]
public string Name { get; set; }
}
It produces this XML :
<?xml version="1.0" encoding="utf-8" ?>
<ShoppingBag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<Fruit name="pineapple" />
<Fruit name="kiwi" />
</Items>
</ShoppingBag>
I don't understand why I am not getting <Items testAttribute="foo">
Please can anyone tell me what I need to add to my code so that the Serializer will write this attribute out?
Thanks,
You need an intermediary class there:
class Program
{
static void Main()
{
var shoppingBag = new ShoppingBag
{
Items = new ShoppingBagItems
{
Fruits = new List<Fruit>(new[] {
new Fruit { Name = "pineapple" },
new Fruit { Name = "kiwi" },
}),
TestAttribute = "foo"
}
};
var serializer = new XmlSerializer(typeof(ShoppingBag));
serializer.Serialize(Console.Out, shoppingBag);
}
}
public class ShoppingBag
{
public ShoppingBagItems Items { get; set; }
}
public class ShoppingBagItems
{
[XmlElement("Fruit")]
public List<Fruit> Fruits { get; set; }
[XmlAttribute("testAttribute")]
public string TestAttribute { get; set; }
}
public class Fruit
{
[XmlAttribute("name")]
public string Name { get; set; }
}
Also note that you don't need to decorate your classes with the [Serializable] attribute as it is used only for binary serialization. Another remark is that you don't need to derive from List<T>, simply use it as a property.
Unfortunately, when serializing a collection the XmlSerializer doesn't take into account the extra properties of that collection. It only considers the members that implement ICollection<T>. If you want to serialize extra attributes, you need to wrap the collection in another class that is not a collection itself.

Categories