XML Deserialization results in empty object - c#

I'm attempting to deserialize this XML file into an object
<?xml version="1.0" encoding="utf-8" ?>
<rules version="3">
<emie>
<domain>msdn.microsoft.com</domain>
<domain exclude="false">
bing.com
<path exclude="true">images</path>
</domain>
<domain exclude="true">
news.msn.com
<path exclude="false">pop-culture</path>
</domain>
<domain>timecard</domain>
<domain>tar</domain>
</emie>
</rules>
I have my objects laid out like so
[XmlRoot("rules")]
public class Rules {
[XmlAttribute("version")]
public string Version { get; set; }
[XmlElement("emie")]
public EMIE EMIE { get; set; }
}
public class EMIE {
[XmlArrayItem("Domain")]
public List<Domain> Domains { get; set; }
}
public class Domain {
[XmlAttribute("exclude")]
public bool Exclude { get; set; }
[XmlText]
public string Value { get; set; }
[XmlArrayItem("Path")]
public List<Path> Paths { get; set; }
}
public class Path {
[XmlAttribute]
public bool Exclude { get; set; }
[XmlText]
public string Value { get; set; }
}
And am using this code to deserialize it
static void Main(string[] args) {
XmlSerializer serializer = new XmlSerializer(typeof(Rules));
using (FileStream stream = new FileStream("EM.xml", FileMode.Open)) {
Rules xml = (Rules)serializer.Deserialize(stream);
foreach (Domain d in xml.EMIE.Domains) {
Console.WriteLine(d.Value);
foreach (EnterpriseModeModel.Path p in d.Paths) {
Console.WriteLine(p.Value);
}
}
}
Console.ReadLine();
}
However, my rules.EMIE.Domains object is always empty. When I debug I can see my stream object has a length so it's properly picking up the data in the file but it never fills up the object like I expect it to.

Change the declaration of EMIE as follows
public class EMIE
{
[XmlElement("domain")]
public List<Domain> Domains { get; set; }
}

Related

Unexpected results when serializing objects to XML

I am having issues creating the schema below...
<DocumentProperties>
<Document>
<Properties>
<propertyName>CNumber</propertyName>
<propertyValue>00645007803</propertyValue>
</Properties>
<targetFolder>\12345678\00645007803\</targetFolder>
</Document>
<Document>
<Properties>
<propertyName>CNumber</propertyName>
<propertyValue>00645007804</propertyValue>
</Properties>
<targetFolder>\12345678\00645007804\</targetFolder>
</Document>
</DocumentProperties>
I created the following classes to do this
public class DocumentProperties
{
public DocumentProperties()
{
Document = new List<Document>();
}
public List<Document> Document { get; set; }
}
public class Document
{
public Document()
{
Properties = new List<Properties>();
}
public List<Properties> Properties { get; set; }
public string targetFolder { get; set; }
}
public class Properties
{
public string propertyName { get; set; }
public string propertyValue { get; set; }
}
public class RetrieveMultipleDocumentsRequest
{
public SystemProperty SystemProperty { get; set; }
public RequestProperty RequestProperty { get; set; }
public DocumentProperties DocumentProperties { get; set; }
}
The output I am getting is "Document" and "Properties" twice as a parent child of each other. How do I resolve this?
Output from my classes
<DocumentProperties>
<Document>
<Document>
<Properties>
<Properties>
<propertyName>DizzzyGelespe</propertyName>
<propertyValue>8E077A60</propertyValue>
</Properties>
<Properties />
</Properties>
<targetFolder>C:\BXml\TargetFolder\</targetFolder>
</Document>
</Document>
</DocumentProperties>
Code that is generating the output:
public string Serialize(RetrieveMultipleDocumentsRequest details)
{
XmlSerializer serializer = new XmlSerializer(typeof(RetrieveMultipleDocumentsRequest));
using(StringWriter textWriter = new StringWriter())
{
serializer.Serialize(textWriter, details);
return textWriter.ToString();
}
}
You will need to annotate your object model as shown below in order to change the default serialization behavior. This application of the XmlElement attribute will prevent emiting out the parent tag based upon the encountered property and instead only emit out the containing data.
public class DocumentProperties
{
public DocumentProperties()
{
Document = new List<Document>();
}
[XmlElement("Document")]
public List<Document> Document { get; set; }
}
public class Document
{
public Document()
{
Properties = new List<Properties>();
}
[XmlElement("Properties")]
public List<Properties> Properties { get; set; }
public string targetFolder { get; set; }
}
Apparently your naming convention confused the XML serializer a bit. Just explicitly decorate the elements as below and it should work fine:
public class DocumentProperties
{
public DocumentProperties()
{
Document = new List<Document>();
}
[XmlElement("Document")]
public List<Document> Document { get; set; }
}
public class Document
{
public Document()
{
Properties = new List<Properties>();
}
[XmlElement("Properties")]
public List<Properties> Properties { get; set; }
public string targetFolder { get; set; }
}
Your problem here is with naming.
A list of documents should be called "documents" not "document", same goes for properties.
If you make these naming changes then you will see that your XML output is correct and makes sense.

XmlSerializer Deserialize list only getting the first item

This is my XML i get from an API:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<datetime>2015-05-18 11:37:32</datetime>
<count>2</count>
<smsleft>40920</smsleft>
<sms><smsid>535041581</smsid><smsid>535041583</smsid></sms>
</response>
This is my class i try to parse it to:
[XmlRoot("response")]
public class SMSResponse
{
[XmlElement("sms")]
public List<smsid> Sms { get; set; }
}
public class smsid
{
[XmlElement("smsid")]
public string SmsID { get; set; }
}
Using this code:
XmlSerializer serializer = new XmlSerializer(typeof(SMSResponse));
using (TextReader reader = new StringReader(response))
{
SMSResponse result = (SMSResponse)serializer.Deserialize(reader);
}
However i only get the first SmsID in the list in my result, not 2 as in the reponse.
What am i doing wrong?
You've declared SmsID as a string, so only a single one can be deserialized. You've declared Sms as a list, but only one exists in your input file.
Try:
[XmlRoot("response")]
public class SMSResponse
{
[XmlArray("sms")]
[XmlArrayItem("smsid")]
public List<string> SmsID { get; set; }
}
Change your code to this
[XmlRoot("response")]
public class SMSResponse
{
[XmlElement("sms")]
public SMS Sms { get; set; }
}
public class SMS
{
[XmlElement("smsid")]
public List<string> SmsID { get; set; }
}
[XmlRoot("response")]
public class SMSResponse
{
[XmlArray(ElementName = "sms")]
[XmlArrayItem(ElementName = "smsid", Type = typeof(smsid))]
public List<smsid> Sms { get; set; }
}
public class smsid
{
[XmlText]
public string SmsID { get; set; }
}

c# deserialize objects issue

This is my input XML file:
<?xml version="1.0" encoding="UTF-8"?>
<hosts>
<host>
<hostId>239|BS|OWN</hostId>
<images>
<image>
<name>Pic.jpg</name>
<main>true</main>
<source>../Images/Melissa/Pic.jpg</source>
</image>
</images>
</host>
</hosts>
and this is my class used to deserialiaze that XML file:
[XmlRoot("hosts")]
public class hosts
{
[XmlElement("host")]
public List<Host> Listehosts { get; set; }
}
public class Host
{
[XmlElement("hostId")]
public string hostId { get; set; }
[XmlElement("images")]
public List<Image> Listeimages { get; set; }
}
public class Image
{
[XmlElement("name")]
public string name { get; set; }
[XmlElement("main")]
public string main { get; set; }
[XmlElement("source")]
public string source { get; set; }
}
And this the code of my main program:
string outputTmp = "Images.xml";
XmlSerializer deserializer = new XmlSerializer(typeof(hosts));
TextReader reader = new StreamReader(outputTmp);
object obj = deserializer.Deserialize(reader);
hosts XmlData = (hosts)obj;
reader.Close();
Console.WriteLine(XmlData.Listehosts.Count);
The problem is that always images list are empty when I execute my program.
The list of hosts is charged correctly but when I checked the list of image it contains permanently null value for all the attribute (name, main, source).
Am I missing something?
Try this :
public class Host
{
[XmlElement("hostId")]
public string hostId { get; set; }
[XmlArray("images")] // CHANGED
[XmlArrayItem("image", typeof(Image))] // CHANGED
public List<Image> Listeimages { get; set; }
}
There is a small mistake in your code, add an ImageCollection class with a list inside.
public class ImageCollection
{
[XmlElement("image")]
public List<Image> Listeimages { get; set; }
}
public class Host
{
[XmlElement("hostId")]
public string hostId { get; set; }
[XmlElement("images")]
public ImageCollection ImageCollection { get; set; }
}

Serialize Property as Xml Attribute in Element

I have the following class:
[Serializable]
public class SomeModel
{
[XmlElement("SomeStringElementName")]
public string SomeString { get; set; }
[XmlElement("SomeInfoElementName")]
public int SomeInfo { get; set; }
}
Which (when populated with some test data) and Serialized using XmlSerializer.Serialize() results in the following XML:
<SomeModel>
<SomeStringElementName>testData</SomeStringElementName>
<SomeInfoElementName>5</SomeInfoElementName>
</SomeModel>
What I need to have is:
<SomeModel>
<SomeStringElementName Value="testData" />
<SomeInfoElementName Value="5" />
</SomeModel>
Is there a way to specify this as attributes without writing my own custom serialization code?
You will need wrapper classes:
public class SomeIntInfo
{
[XmlAttribute]
public int Value { get; set; }
}
public class SomeStringInfo
{
[XmlAttribute]
public string Value { get; set; }
}
public class SomeModel
{
[XmlElement("SomeStringElementName")]
public SomeStringInfo SomeString { get; set; }
[XmlElement("SomeInfoElementName")]
public SomeIntInfo SomeInfo { get; set; }
}
or a more generic approach if you prefer:
public class SomeInfo<T>
{
[XmlAttribute]
public T Value { get; set; }
}
public class SomeModel
{
[XmlElement("SomeStringElementName")]
public SomeInfo<string> SomeString { get; set; }
[XmlElement("SomeInfoElementName")]
public SomeInfo<int> SomeInfo { get; set; }
}
And then:
class Program
{
static void Main()
{
var model = new SomeModel
{
SomeString = new SomeInfo<string> { Value = "testData" },
SomeInfo = new SomeInfo<int> { Value = 5 }
};
var serializer = new XmlSerializer(model.GetType());
serializer.Serialize(Console.Out, model);
}
}
will produce:
<?xml version="1.0" encoding="ibm850"?>
<SomeModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SomeStringElementName Value="testData" />
<SomeInfoElementName Value="5" />
</SomeModel>
Kind of, use the XmlAttribute instead of XmlElement, but it won't look like what you want. It will look like the following:
<SomeModel SomeStringElementName="testData">
</SomeModel>
The only way I can think of to achieve what you want (natively) would be to have properties pointing to objects named SomeStringElementName and SomeInfoElementName where the class contained a single getter named "value". You could take this one step further and use DataContractSerializer so that the wrapper classes can be private. XmlSerializer won't read private properties.
// TODO: make the class generic so that an int or string can be used.
[Serializable]
public class SerializationClass
{
public SerializationClass(string value)
{
this.Value = value;
}
[XmlAttribute("value")]
public string Value { get; }
}
[Serializable]
public class SomeModel
{
[XmlIgnore]
public string SomeString { get; set; }
[XmlIgnore]
public int SomeInfo { get; set; }
[XmlElement]
public SerializationClass SomeStringElementName
{
get { return new SerializationClass(this.SomeString); }
}
}

Xml deserialization with nested tags not working

I need to deserialize a XML file to a object. Following is the XML content:
<?xml version="1.0" encoding="utf-8" ?>
<PdfFile>
<PageTitle DocumentName="Sequence Diagram" Version="Version 4" >Title</PageTitle>
<LogoPath>C:\logo.png</LogoPath>
<Modules>
<Module Id="1" MainTitle="Module1">
<SubModules>
<SubModule>
<Title>SubModule1</Title>
<Path>SubModule1 Path</Path>
<Description>SubModule1 Desc</Description>
</SubModule>
<SubModule>
<Title>SubModule2</Title>
<Path>SubModule2 Path</Path>
<Description>SubModule2 Desc</Description>
</SubModule>
</SubModules>
</Module>
<Module Id="2" MainTitle="Module2">
<SubModules>
<SubModule>
<Title>SubModule1</Title>
<Path>SubModule1 Path</Path>
<Description>SubModule1 Desc</Description>
</SubModule>
</SubModules>
</Module>
</Modules>
</PdfFile>
Following is the class file I created, for the above xml file.
using System;
using System.Xml.Serialization;
namespace PDFCreation.Objects
{
[Serializable]
[XmlRoot("PdfFile")]
public class PdfFile2
{
[XmlElement("PageTitle")]
public PageTitle FirstPage { get; set; }
[XmlElement("LogoPath")]
public string LogoPath { get; set; }
[XmlArray("Modules")]
[XmlArrayItem("Module", typeof(Module))]
public Module[] Modules { get; set; }
}
[Serializable]
public class Module
{
[XmlAttributeAttribute("Id")]
public int Id { get; set; }
[XmlAttributeAttribute("MainTitle")]
public string MainTitle { get; set; }
[XmlArray("SubModules")]
[XmlArrayItem("SubModule", typeof(SubModule))]
public SubModule[] Modules { get; set; }
}
[Serializable]
public class SubModule
{
[XmlElement("Title")]
public string Title { get; set; }
[XmlElement("Path")]
public string Path { get; set; }
[XmlElement("Description")]
public string Description { get; set; }
}
[Serializable]
public class PageTitle
{
[XmlText]
public string Title { get; set; }
[XmlAttribute]
public string DocumentName { get; set; }
[XmlAttribute]
public string Version { get; set; }
}
}
On deserializing, I'm not getting any error. But the modules inside PdfFile object always returns null. I tried to use the generated class from xsd.exe. But still the same thing is happening.
Please help me to find issue in the code/xml and why it is not deserializing fully?
Thanks!!!
Edited:
My C# code:
public class Program
{
private static readonly string XmlPath = ConfigurationManager.AppSettings["XmlPath"];
static void Main(string[] args)
{
try
{
ReadXml();
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
Console.ReadLine();
}
}
private static void ReadXml()
{
if (!File.Exists(XmlPath))
{
Console.WriteLine("Error: Xml File Not exists in the path: {0}", XmlPath);
return;
}
using (var reader = new StreamReader(XmlPath))
{
var serializer = new XmlSerializer(typeof(PdfFile2));
var result = (PdfFile2)serializer.Deserialize(reader);
//other code here
}
}
}
Using nothing but you supplied code/xml and putting in the missing closing tags I got it to deserialize using this Main-class:
using System.IO;
using System.Xml.Serialization;
using PDFCreation.Objects;
public class Test
{
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(PdfFile2));
PdfFile2 pdf = (PdfFile2)ser.Deserialize(File.OpenRead("test.xml"));
}
}
What does your deserialization code look like?

Categories