Deserialize Multiple Xml Elements With the Same Name Through Xmlserializer Class in C#

Deserialize multiple XML elements with the same name through XmlSerializer class in C#

You don't want XmlArrayItem. You want the array of ints to be serialized without a parent element, which means you should decorate the array itself with XmlElement. Because you have a particular order, you will want to use the Order value on the XmlElement attribute. Here's the class, modified accordingly:

public class BackupScheduleSettings
{
public BackupScheduleSettings()
{
ScheduledDay = new int[7];
}

[XmlElement(Order=1)]
public int AggressiveMode;
[XmlElement(Order=2)]
public int ScheduleType;
//[XmlArrayItem("ArrayWrapper")]
[XmlElement(Order=3)]
public int[] ScheduledDay { get; set; }
[XmlElement(Order=4)]
public int WindowStart;
[XmlElement(Order=5)]
public int WindowEnd;
[XmlElement(Order=6)]
public int ScheduleInterval;
}

Here's the generated xML:

<BackupScheduleSettings>
<AggressiveMode>0</AggressiveMode>
<ScheduleType>0</ScheduleType>
<ScheduledDay>0</ScheduledDay>
<ScheduledDay>0</ScheduledDay>
<ScheduledDay>0</ScheduledDay>
<ScheduledDay>0</ScheduledDay>
<ScheduledDay>0</ScheduledDay>
<ScheduledDay>0</ScheduledDay>
<ScheduledDay>0</ScheduledDay>
<WindowStart>0</WindowStart>
<WindowEnd>0</WindowEnd>
<ScheduleInterval>0</ScheduleInterval>
</BackupScheduleSettings>

Deserialize multiple XML elements with the same name in C#

This works, just make wind an array:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindDeserialize
{
using System.IO;
using System.Xml.Serialization;

class Program
{
public const string xml = "<?xml version=\"1.0\" encoding=\"utf-16\"?> "+
"<CurrentWeather> "+
" <Location>BOSTON LOGAN INTERNATIONAL, MA, United States (KBOS) 42-22N 071-01W 54M</Location> "+
" <Time>Feb 11, 2015 - 11:54 AM EST / 2015.02.11 1654 UTC</Time> "+
" <Wind> from the N (010 degrees) at 17 MPH (15 KT) gusting to 26 MPH (23 KT):0</Wind> "+
" <Visibility> 2 mile(s):0</Visibility> "+
" <SkyConditions> overcast</SkyConditions> "+
" <Temperature> 19.9 F (-6.7 C)</Temperature> "+
" <Wind>Windchill: 5 F (-15 C):1</Wind> "+
" <DewPoint> 12.9 F (-10.6 C)</DewPoint> "+
" <RelativeHumidity> 73%</RelativeHumidity> "+
" <Pressure> 30.08 in. Hg (1018 hPa)</Pressure> "+
" <Status>Success</Status> "+
"</CurrentWeather> ";
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(CurrentWeather));

var weather = (CurrentWeather)ser.Deserialize(new StringReader(xml));

}


}

public class CurrentWeather
{
[XmlElement]
public string Location { get; set; }
[XmlElement]
public string Time { get; set; }
[XmlElement]
public string [] Wind { get; set; }
[XmlElement]
public string Visibility { get; set; }
[XmlElement]
public string SkyConditions { get; set; }
[XmlElement]
public string Temperature { get; set; }
[XmlElement]
public string DewPoint { get; set; }
[XmlElement]
public string RelativeHumidity { get; set; }
[XmlElement]
public string Pressure { get; set; }
[XmlElement]
public string Status { get; set; }
}
}

Deserialize XML same items with different tag

Use IXmlSerializable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml.Linq;

namespace ConsoleApplication2
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(XML_Settings));
XML_Settings settings = (XML_Settings)serializer.Deserialize(reader);

}
}
[XmlRoot("Settings")]
public class XML_Settings
{


[XmlElement(ElementName = "FileName")]
public string FileName { get; set; }

[XmlElement(ElementName = "Zones")]
public Zones_Points Zones_Point { get; set; }
[XmlElement(ElementName = "Network_IP")]
public string NetworkIP { get; set; }
[XmlElement(ElementName = "Ethernet_Enable")]
public bool EthernetEnable { get; set; }
[XmlElement(ElementName = "Language")]
public int Language { get; set; }
}
public partial class Zones_Points : IXmlSerializable
{
// Xml Serialization Infrastructure
[XmlElement(ElementName = "Zones")]
public List<Zones_Point> Points { get; set; }

public void WriteXml(XmlWriter writer)
{
}

public void ReadXml(XmlReader reader)
{

XElement zones = (XElement)XElement.ReadFrom(reader);
Points = new List<Zones_Point>();
foreach (XElement row in zones.Elements())
{
Zones_Point Point = new Zones_Point();
Points.Add(Point);
Point.Address = (int)row.Element("Address");
Point.PointZone = (int)row.Element("Zone");
Point.Installed = (Boolean)row.Element("Installed");
}

}

public XmlSchema GetSchema()
{
return (null);
}
}
public partial class Zones_Point
{
[XmlElement(ElementName = "Address")]
public int Address { get; set; }
[XmlElement(ElementName = "Zone")]
public int PointZone { get; set; }
[XmlElement(ElementName = "Installed")]
public bool Installed { get; set; }
}
}

How to deserialize XML file with nested elements of same name, with one of elements are root?

You can see in this fiddle that if I take your code and run it... it works!

What I would suggest, however, is to have two classes: one for the 'root' and one for each child element. This would make it less confusing to work with:

[XmlRoot("Employee", Namespace = "http://www.testxmlns.com/employee")]
public class EmployeeRoot
{
[XmlElement("Employee")]
public Employee[] Employees { get; set; }
}

public class Employee
{
public string OtherElement { get; set; }
}

You can see in this fiddle that this also works.

Deserialize XML elements with different type name in C#

I got it to work by serializing some test data and getting it to compare with you input xml data. See code below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string FILENAME1 = @"c:\temp\test1.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
StringReader sReader = new StringReader(xml);
XmlReader xReader = XmlReader.Create(sReader);
XmlSerializer serializer = new XmlSerializer(typeof(EmployeeAllocationResponse));

EmployeeAllocationResponse r = new EmployeeAllocationResponse()
{
Allocatables = new List<Allocatables>() {
new Duty(),
new Absence()
}
};
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME1, settings);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("ns2", "http://employeeallocation.intf.mb.ivu.de/fault");
ns.Add("ns3", "http://employeeallocation.intf.mb.ivu.de/");
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
serializer.Serialize(writer, r, ns);

EmployeeAllocationResponse response = (EmployeeAllocationResponse)serializer.Deserialize(xReader);

}
}
[XmlRoot(ElementName = "EmployeeAllocationResponse", Namespace = "http://employeeallocation.intf.mb.ivu.de/")]
public class EmployeeAllocationResponse
{
[XmlArray(ElementName = "employeeAllocation", Namespace = "")]
[XmlArrayItem(ElementName = "allocatables", Namespace = "")]
public List<Allocatables> Allocatables { get; set; }
}
[XmlRoot(ElementName = "allocatables", Namespace = "http://employeeallocation.intf.mb.ivu.de/")]
[XmlInclude(typeof(Duty)), XmlInclude(typeof(Absence))]
public class Allocatables
{
[XmlElement(ElementName = "startTime", Namespace = "")]
public DateTime startTime { get; set; }
[XmlElement(ElementName = "endTime", Namespace = "")]
public DateTime endTime { get; set; }
}
[XmlRoot(ElementName = "Duty", Namespace = "http://employeeallocation.intf.mb.ivu.de/")]
public class Duty : Allocatables
{
[XmlElement(ElementName = "shortName", Namespace = "")]
public string shortName { get; set; }
[XmlElement(ElementName = "rosterAbbreviation", Namespace = "")]
public string rosterAbbreviation { get; set; }
}
[XmlRoot(ElementName = "Absence", Namespace = "http://employeeallocation.intf.mb.ivu.de/")]
public class Absence : Allocatables
{
[XmlElement(ElementName = "typeAbbreviation", Namespace = "")]
public string typeAbbreviation { get; set; }
[XmlElement(ElementName = "typeLongname", Namespace = "")]
public string typeLongname { get; set; }
[XmlElement(ElementName = "type", Namespace = "")]
public string type { get; set; }
}
}

C# xml serialization of multiple elements with same element name

Use this classes:

public enum DateType
{
Foo, Bar, Baz
}

[XmlRoot(ElementName = "date")]
public class Date
{
[XmlAttribute(AttributeName = "type")]
public DateType Type { get; set; }

[XmlText]
public string Value { get; set; }
}

[XmlRoot(ElementName = "offer")]
public class Offer
{
[XmlElement(ElementName = "date")]
public Date[] Dates { get; set; }
}

And deserialize with:

string lsXml = @"<offer>
<date type=""Foo"">Some value 1</date>
<date type=""Bar"">Some value 2</date>
<date type=""Baz"">Some value 3</date>
</offer>";

XmlSerializer loXmlSerializer = new XmlSerializer(typeof(Offer));
var loOffer = loXmlSerializer.Deserialize(new StringReader(lsXml)) as Offer;
foreach (var loDate in loOffer.Dates)
{
Console.WriteLine($"{loDate.Type}: {loDate.Value}");
}


Related Topics



Leave a reply



Submit