apologies for the newbie question as I am very new to the world of programming etc.
I have a large XML file containing (see link below). It contains an ID number for legal entities (LEI) followed by the ID for their respective parent company.
Example from the XML file
The yellow is the entity LEI number and the green is the parent company LEI.
I would like to create some sort of batch script or GUI so I can enter a list of entity (green) LEI numbers and then given an output of all the corresponding parent LEI numbers.
Here is the file for anyone wondering: https://leidata.gleif.org/api/v1/concatenated-files/rr/20171025/zip
I am very inexperienced so I am not sure where to start.
Many thanks
Copy the entire file content to clipboard, then open your visual studio project and go to menu -> edit -> paste special -> paste as xml classes. This will generate a bunch of classes in the file you have currently open. Save this file. Then you use this code to load the file and return the loaded data:
public static RelationshipData LoadFile(string fileName)
{
var serializer = new XmlSerializer(typeof(Items));
RelationshipData data;
using (Stream r = new FileStream(filename, FileMode.Open))
{
data = (RelationshipData)serializer.Deserialize(r);
}
return data;
}
Now you should be able to access the file content using the properties of the returned data object. Haven't used this method with xml-data that uses prefixes, but I think this will be handled too...
With this data you should build up a dictionary or something like that that suits your needs that maps one ID to the other ID.
You should really start reading why and how this works...
NOT TESTED
I think I got most of the elements. You need to do some careful checking since there are a lot of nodes. I added a writer so I can use Beyond Compare to check if I got all the nodes. Fixed some errors. Still need some minor tweaks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_FILENAME = #"c:\temp\test.xml";
const string OUTPUT_FILENAME = #"c:\temp\test1.xml";
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.CheckCharacters = false;
XmlReader reader = XmlReader.Create(INPUT_FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(RelationshipDataRelationshipData));
RelationshipDataRelationshipData relationshipDataRelationshipData = (RelationshipDataRelationshipData)serializer.Deserialize(reader);
XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Indent = true;
XmlWriter writer = XmlWriter.Create(OUTPUT_FILENAME, wSettings);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("rr", "http://www.gleif.org/data/schema/rr/2016");
ns.Add("gleif", "http://www.gleif.org/concatenated-file/header-extension/2.0");
serializer.Serialize(writer, relationshipDataRelationshipData, ns);
writer.Flush();
writer.Close();
}
}
[XmlRoot(ElementName = "RelationshipData", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipDataRelationshipData
{
[XmlElement("Header")]
public Header header { get; set; }
[XmlElement("RelationshipRecords", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public RelationshipRecords relationshipRecords { get; set; }
}
[XmlRoot(ElementName = "Header", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Header
{
[XmlElement("ContentDate")]
public DateTime ContentDate { get; set; }
[XmlElement("FileContent")]
public string FileContent { get; set; }
[XmlElement("RecordCount")]
public int RecordCount { get; set; }
[XmlElement("Extension", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public Extension extension { get; set; }
}
[XmlRoot(ElementName = "Extension", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Extension
{
[XmlElement(ElementName = "Sources", Namespace = "http://www.gleif.org/concatenated-file/header-extension/2.0")]
public Sourses sources { get; set; }
}
[XmlRoot(ElementName = "Sources", Namespace = "http://www.gleif.org/concatenated-file/header-extension/2.0")]
public class Sourses
{
[XmlElement("Source")]
public List<Source> source { get; set; }
}
[XmlRoot(ElementName = "Source", Namespace = "http://www.gleif.org/concatenated-file/header-extension/2.0")]
public class Source
{
[XmlElement("ContentDate")]
public DateTime ContentDate { get; set; }
[XmlElement("Originator")]
public string Originator { get; set; }
[XmlElement("RecordCount")]
public int RecordCount { get; set; }
}
[XmlRoot(ElementName = "RelationshipRecords", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipRecords
{
[XmlElement("RelationshipRecord")]
public List<RelationshipRecord> relationshipRecord { get; set; }
}
[XmlRoot(ElementName = "RelationshipRecord", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipRecord
{
[XmlElement("Relationship")]
public Relationship relationship { get; set; }
[XmlElement("Registration")]
public Registration registration { get; set; }
[XmlElement("Extension", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public Extension extension { get; set; }
}
[XmlRoot(ElementName = "Relationship", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Relationship
{
[XmlElement("StartNode")]
public Node StartNode { get; set; }
[XmlElement("EndNode")]
public Node EndNode { get; set; }
public string RelationshipType { get; set; }
[XmlElement("RelationshipPeriods")]
public RelationshipPeriods relationshipPeriods { get; set; }
public string RelationshipStatus { get; set; }
[XmlElement("RelationshipQualifiers")]
public RelationshipQualifiers relationshipQualifiers { get; set; }
}
[XmlRoot(ElementName = "Node", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Node
{
public string NodeID { get; set; }
public string NodeIDType { get; set; }
}
[XmlRoot(ElementName = "class RelationshipQualifiers", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipQualifiers
{
[XmlElement("RelationshipQualifier")]
public RelationshipQualifier relationshipQualifier { get; set; }
}
[XmlRoot(ElementName = "class RelationshipQualifier", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipQualifier
{
public string QualifierDimension { get; set; }
public string QualifierCategory { get; set; }
}
[XmlRoot(ElementName = "Registration", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class Registration
{
public DateTime InitialRegistrationDate { get; set; }
public DateTime LastUpdateDate { get; set; }
public string RegistrationStatus { get; set; }
public DateTime NextRenewalDate { get; set; }
public string ManagingLOU { get; set; }
public string ValidationSources { get; set; }
public string ValidationDocuments { get; set; }
}
[XmlRoot(ElementName = "class RelationshipPeriods", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipPeriods
{
[XmlElement("RelationshipPeriod")]
public List<RelationshipPeriod> relationshipPeriod { get; set; }
}
[XmlRoot(ElementName = "class RelationshipPeriod", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipPeriod
{
[XmlElement("StartDate")]
public DateTime StartDate { get; set; }
[XmlElement("EndDate")]
public DateTime EndDate { get; set; }
[XmlElement("PeriodType")]
public string PeriodType { get; set; }
}
[XmlRoot(ElementName = "class RelationshipQuantifiers", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipQuantifiers
{
[XmlElement("RelationshipQuantifier")]
public List<RelationshipQuantifier> relationshipQuantifier { get; set; }
}
[XmlRoot(ElementName = "class RelationshipQuantifier", Namespace = "http://www.gleif.org/data/schema/rr/2016")]
public class RelationshipQuantifier
{
[XmlElement("MeasurementMethod")]
public DateTime MeasurementMethod { get; set; }
[XmlElement("QuantifierAmount")]
public decimal QuantifierAmount { get; set; }
[XmlElement("QuantifierUnits")]
public string QuantifierUnits { get; set; }
}
}
Related
This is an XML example I want to deserialize and use in a Desktop App, where the user could insert an XML like this one:
<Settings>
<FileName>C:\Users\myniceuser\Desktop\hello.xmldoc</FileName>
<Zones>
<Row1>
<Address>2</Address>strong text
<Zone>2</Zone>
<Installed>True</Installed>
</Row1>
<Row2>
<Address>3</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row2>
<Row3>
<Address>4</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row3>
<Row4>
<Address>5</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row4>
</Zones>
<Network_IP>010.000.008.072</Network_IP>
<Ethernet_Enable>true</Ethernet_Enable>
<Language>1</Language>
</Settings>
The class I implemented is this one:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace MyApp.Models
{
[XmlRoot(ElementName = "Settings")]
public class XML_Settings
{
[XmlElement(ElementName = "FileName")]
public string FileName { get; set; }
//[XmlElement(ElementName = "Zones")]
[XmlElement(ElementName ="Zones")]
public Zones_Points[] Zones_Points { 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; }
}
[XmlRoot(ElementName = "")]
public partial class Zones_Points
{
[XmlElement(ElementName ="Address")]
public int Address { get; set; }
[XmlElement(ElementName ="Zone")]
public int PointZone { get; set; }
[XmlElement(ElementName ="Installed")]
public bool Installed { get; set; }
}
}
I deserialize like this:
Models.XML_Settings inputted_xml = new();
using StreamReader stream_xml = new StreamReader(xmlfile_path, CodePagesEncodingProvider.Instance.GetEncoding(1253));
XmlSerializer serializer = new XmlSerializer(typeof(Models.XML_Settings));
try
{
Models.XML_Settings input = (Models.XML_GR6500)serializer.Deserialize(stream_xml);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
I want my app to input files in XML like this one, and convert their data to a class that i can use.
The object "input" created by the Deserialization got every data correct, except the Zones, those that in XML have the "Row" tags. Those "rows" could every time be more or less that 4.
What can I do to have the items with the row tags deserialized and inputted into a Zones_Points list or array?
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 document
My XML
<Title>
<NAME>ABC</NAME>
<BODY>
<A>IAG4</A>
<B>
<B1>
<C>100</C>
<C>EC0001</C1>
<C2>DEF</C2>
<C3>100</C3>
<C4>200</C4>
<C5>600</C5>
<C6>1000</C6>
</B1>
<B1>
<D>101</D>
<D1>EC0002</D1>
</B1>
</B>
</BODY>
</Title>
I want to deserialize this into a class and I want to access them with the objects of the class created. I am using C#.
Use xml serialization code as shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Message));
Message message = (Message)serializer.Deserialize(reader);
}
}
[XmlRoot("MESSAGE")]
public class Message
{
[XmlElement("NAME")]
public string name { get; set; }
[XmlElement("BODY")]
public Body body { get; set; }
}
public class Body
{
[XmlElement("EQPID")]
public string eqpid { get; set; }
[XmlArray("ECS")]
[XmlArrayItem("EC")]
public List<EC> ec { get; set; }
}
public class EC
{
public int ECID { get; set; }
public string ECNAME { get; set; }
public string ECDEF { get; set; }
public int ECSLL { get; set; }
public int ECSUL { get; set; }
public int ECWLL { get; set; }
public int ECWUL { get; set; }
}
}
You could use online tools like Xml2Csharp to generate classes needed to store your XML Data. To deserialize the Xml, you could then use the XmlSerializer
XmlSerializer serializer = new XmlSerializer(typeof(MESSAGE));
using (TextReader reader = new StringReader(xml))
{
var result = (MESSAGE)serializer.Deserialize(reader);
}
Where your classes are defined as
[XmlRoot(ElementName="EC")]
public class EC
{
[XmlElement(ElementName="ECID")]
public string ECID { get; set; }
[XmlElement(ElementName="ECNAME")]
public string ECNAME { get; set; }
[XmlElement(ElementName="ECDEF")]
public string ECDEF { get; set; }
[XmlElement(ElementName="ECSLL")]
public string ECSLL { get; set; }
[XmlElement(ElementName="ECSUL")]
public string ECSUL { get; set; }
[XmlElement(ElementName="ECWLL")]
public string ECWLL { get; set; }
[XmlElement(ElementName="ECWUL")]
public string ECWUL { get; set; }
}
[XmlRoot(ElementName="ECS")]
public class ECS
{
[XmlElement(ElementName="EC")]
public List<EC> EC { get; set; }
}
[XmlRoot(ElementName="BODY")]
public class BODY
{
[XmlElement(ElementName="EQPID")]
public string EQPID { get; set; }
[XmlElement(ElementName="ECS")]
public ECS ECS { get; set; }
}
[XmlRoot(ElementName="MESSAGE")]
public class MESSAGE
{
[XmlElement(ElementName="NAME")]
public string NAME { get; set; }
[XmlElement(ElementName="BODY")]
public BODY BODY { get; set; }
}
I'm get this XML as a response to a rest call. The original list comes back populated fine but the list within the CollectorDate class returns null for all properties. How do I get this to Deserialize properly?
<CollectorHeardMIUs>
<CollectorDate Date="2018/07/16" CollectorId="11090_5000">
<CollectorLatitude></CollectorLatitude>
<CollectorLongitude></CollectorLongitude>
<Miu MiuId="1461860710">
<PremiseID>859869749</PremiseID>
<PremiseAccount>Unknown</PremiseAccount>
<PremiseLatitude>39.31553</PremiseLatitude>
<PremiseLongitude>-84.608627</PremiseLongitude>
</Miu>
<Miu MiuId="1478541235">
<PremiseID>859251478</PremiseID>
<PremiseAccount>Unknown</PremiseAccount>
<PremiseLatitude>39.36231</PremiseLatitude>
<PremiseLongitude>-84.54222</PremiseLongitude>
</Miu>
</CollectorDate>
<CollectorDate Date="2018/07/17" CollectorId="11090_5000">
<CollectorLatitude></CollectorLatitude>
<CollectorLongitude></CollectorLongitude>
<Miu MiuId="1461860710">
<PremiseID>859869749</PremiseID>
<PremiseAccount>Unknown</PremiseAccount>
<PremiseLatitude>39.31553</PremiseLatitude>
<PremiseLongitude>-84.608627</PremiseLongitude>
</Miu>
</CollectorDate>
</CollectorHeardMIUs>
<CollectorHeardMIUs>
<CollectorDate Date="2018/07/16" CollectorId="11090_5000">
<CollectorLatitude></CollectorLatitude>
<CollectorLongitude></CollectorLongitude>
<Miu MiuId="1461860710">
<PremiseID>859869749</PremiseID>
<PremiseAccount>Unknown</PremiseAccount>
<PremiseLatitude>39.31553</PremiseLatitude>
<PremiseLongitude>-84.608627</PremiseLongitude>
</Miu>
</CollectorDate>
<CollectorDate Date="2018/07/17" CollectorId="11090_5000">
<CollectorLatitude></CollectorLatitude>
<CollectorLongitude></CollectorLongitude>
<Miu MiuId="1461860710">
<PremiseID>859869749</PremiseID>
<PremiseAccount>Unknown</PremiseAccount>
<PremiseLatitude>39.31553</PremiseLatitude>
<PremiseLongitude>-84.608627</PremiseLongitude>
</Miu>
</CollectorDate>
</CollectorHeardMIUs>
Here are my classes:
public class CollectorDate
{
public string Date { get; set; }
public string CollectorId{ get; set; }
public decimal? CollectorLatitude { get; set; }
public decimal? CollectorLongitude { get; set; }
public List<Miu> Miu { get; set; }
}
public class Miu
{
public string MiuId { get; set; }
public string PremiseID { get; set; }
public string PremiseAccount { get; set; }
public decimal? PremiseLatitude { get; set; }
public decimal? PremiseLongitude { get; set; }
}
And my RestSharp method:
static List<CollectorDate> GetCollectorHeardMius(string token, DateTime startdt, DateTime enddt, string collectorId)
{
var client = new RestClient(ConfigurationManager.AppSettings.Get("ApiURL"));
var request = new RestRequest("collector_heard_mius");
request.AddParameter("token", token);
request.AddParameter("site_id", ConfigurationManager.AppSettings.Get("SiteId"));
request.AddParameter("start_date", startdt.ToString("yyyy/MM/dd"));
request.AddParameter("end_date", enddt.ToString("yyyy/MM/dd"));
request.AddParameter("collector_id", collectorId);
var response = client.Execute<List<CollectorDate>>(request);
return response.Data;
}
I have tried adding a class for the root element of CollectorHeardMIUs which only contains a single property of List CollectorDate but that returns a List with all empty properties.
I think the issue is with there not being a named tag for the lists of CollectorDate and Miu that is separate but I can't figure out how to handle it.
You are not able to get root Element use this code:
So I have Created a Test Application And this is Tested
So just Append it and then Deserialize it and check my model also
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//I am simply loading data From this file you can simply replace with data which are getting from rest Api
string data = File.ReadAllText("D:\\xml.txt");
string appender = "<root>" + data + "</root>";
XmlSerializer xmldata = new XmlSerializer(typeof(Root));
byte[] byteArray = Encoding.ASCII.GetBytes(data);
MemoryStream stream = new MemoryStream(byteArray);
object datafromXml = xmldata.Deserialize(stream);
}
}
[XmlRoot(ElementName = "Miu")]
public class Miu
{
[XmlElement(ElementName = "PremiseID")]
public string PremiseID { get; set; }
[XmlElement(ElementName = "PremiseAccount")]
public string PremiseAccount { get; set; }
[XmlElement(ElementName = "PremiseLatitude")]
public string PremiseLatitude { get; set; }
[XmlElement(ElementName = "PremiseLongitude")]
public string PremiseLongitude { get; set; }
[XmlAttribute(AttributeName = "MiuId")]
public string MiuId { get; set; }
}
[XmlRoot(ElementName = "CollectorDate")]
public class CollectorDate
{
[XmlElement(ElementName = "CollectorLatitude")]
public string CollectorLatitude { get; set; }
[XmlElement(ElementName = "CollectorLongitude")]
public string CollectorLongitude { get; set; }
[XmlElement(ElementName = "Miu")]
public List<Miu> Miu { get; set; }
[XmlAttribute(AttributeName = "Date")]
public string Date { get; set; }
[XmlAttribute(AttributeName = "CollectorId")]
public string CollectorId { get; set; }
}
[XmlRoot(ElementName = "CollectorHeardMIUs")]
public class CollectorHeardMIUs
{
[XmlElement(ElementName = "CollectorDate")]
public List<CollectorDate> CollectorDate { get; set; }
}
[XmlRoot(ElementName = "root")]
public class Root
{
[XmlElement(ElementName = "CollectorHeardMIUs")]
public List<CollectorHeardMIUs> CollectorHeardMIUs { get; set; }
}
}
I am trying to deserialize an XML that looks like this:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<ns2:ECReports xmlns:ns2="urn:epcglobal:ale:xsd:1" xmlns:ns3="urn:epcglobal:ale:wsdl:1" specName="Cycle_MDEAirport_1" date="2016-04-25T15:06:19.980Z"
ALEID="RIFIDI-ALE1158647263" totalMilliseconds="9492" terminationCondition="DURATION">
<reports>
<report reportName="Cycle_MDEAirport">
<group>
<groupList>
<member>
<epc>303400c0e4a3f48000a2f8d5</epc>
</member>
</groupList>
</group>
</report>
</reports>
<ECSpec includeSpecInReports="true">
<logicalReaders>
<logicalReader>MDEAirport</logicalReader>
</logicalReaders>
<boundarySpec>
<repeatPeriod unit="MS">10000</repeatPeriod>
<duration unit="MS">9500</duration>
<stableSetInterval unit="MS">0</stableSetInterval>
</boundarySpec>
<reportSpecs>
<reportSpec reportName="Cycle_MDEAirport" reportIfEmpty="true" reportOnlyOnChange="false">
<reportSet set="ADDITIONS" />
<output includeEPC="true" includeTag="true" includeRawHex="true" includeRawDecimal="true" />
</reportSpec>
</reportSpecs>
</ECSpec>
</ns2:ECReports>
But I get the following error:
<ImplementationException xmlns='urn:epcglobal:ale:wsdl:1'> was not expected
Here is my code:
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "ECReports";
xRoot.Namespace = "urn:epcglobal:ale:xsd:1";
xRoot.IsNullable = true;
XmlSerializer serializer = new XmlSerializer(typeof(ECReports), xRoot);
MemoryStream ms = new MemoryStream(e.Message);
ECReports ECReports;
ECReports = (ECReports)serializer.Deserialize(ms);
The class ECReports that I am using is from a Service Reference (it is not a class developed by me)
I found the issue. You are going from a namespace to a non-namespace so you have to specify the namespace as "".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
byte[] message = File.ReadAllBytes(FILENAME);
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "ECReports";
xRoot.Namespace = "urn:epcglobal:ale:xsd:1";
xRoot.IsNullable = true;
XmlSerializer serializer = new XmlSerializer(typeof(ECReports), xRoot);
MemoryStream ms = new MemoryStream(message);
ECReports ECReports;
ECReports = (ECReports)serializer.Deserialize(ms);
}
}
[XmlRoot("ECReports")]
public class ECReports
{
[XmlAttribute("specName")]
public string specName { get; set; }
[XmlAttribute("date")]
public DateTime date { get; set; }
[XmlAttribute("ALEID")]
public string aleid { get; set; }
[XmlAttribute("totalMilliseconds")]
public int totalMilliseconds { get; set; }
[XmlAttribute("terminationCondition")]
public string terminationCondition { get; set; }
[XmlElement(ElementName = "reports", Namespace = "")]
public Reports reports { get; set; }
[XmlElement(ElementName = "ECSpec", Namespace = "")]
public ECSpec ecSpec { get; set; }
}
[XmlRoot(ElementName = "reports", Namespace = "")]
public class Reports
{
[XmlElement("report")]
public Report report { get; set; }
}
[XmlRoot("report")]
public class Report
{
[XmlAttribute("reportName")]
public string reportName { get; set; }
[XmlElement("group")]
public Group group { get; set; }
}
[XmlRoot("group")]
public class Group
{
[XmlElement("groupList")]
public GroupList groupList { get; set; }
}
[XmlRoot("groupList")]
public class GroupList
{
[XmlElement("member")]
public Member member { get; set; }
}
[XmlRoot("member")]
public class Member
{
[XmlElement("epc")]
public string epc { get; set; }
}
[XmlRoot("ECSpec", Namespace = "")]
public class ECSpec
{
[XmlAttribute("includeSpecInReports")]
public Boolean includeSpecInReports { get; set; }
[XmlElement("logicalReaders")]
public LogicalReaders logicalReaders { get; set; }
[XmlElement("boundarySpec")]
public BoundarySpec boundarySpec { get; set; }
[XmlElement("reportSpecs")]
public ReportSpecs reportSpecs { get; set; }
}
[XmlRoot("logicalReaders")]
public class LogicalReaders
{
[XmlElement("logicalReader")]
public string logicalReader { get; set; }
}
[XmlRoot("boundarySpec")]
public class BoundarySpec
{
[XmlElement("repeatPeriod")]
public Unit repeatPeriod { get; set; }
[XmlElement("duration")]
public Unit duration { get; set; }
[XmlElement("stableSetInterval")]
public Unit stableSetInterval { get; set; }
}
[XmlRoot("reportSpecs")]
public class ReportSpecs
{
[XmlElement("reportSpec")]
public ReportSpec reportSpec { get; set; }
}
[XmlRoot("")]
public class Unit
{
[XmlAttribute("unit")]
public string unit { get; set; }
[XmlText]
public int value { get; set; }
}
[XmlRoot("reportSpec")]
public class ReportSpec
{
[XmlAttribute("reportName")]
public string reportName { get; set; }
[XmlAttribute("reportIfEmpty")]
public Boolean reportIfEmpty { get; set; }
[XmlAttribute("reportOnlyOnChange")]
public Boolean reportOnlyOnChange { get; set; }
[XmlElement("reportSet")]
public ReportSet reportSet { get; set; }
[XmlElement("output")]
public Output output { get; set; }
}
[XmlRoot("reportSet")]
public class ReportSet
{
[XmlAttribute("set")]
public string set { get; set; }
}
[XmlRoot("output")]
public class Output
{
[XmlAttribute("includeEPC")]
public Boolean includeEPC { get; set; }
[XmlAttribute("includeTag")]
public Boolean includeTag { get; set; }
[XmlAttribute("includeRawHex")]
public Boolean includeRawHex { get; set; }
[XmlAttribute("includeRawDecimal")]
public Boolean includeRawDecimal { get; set; }
}
}
VS2008, .NET Framework 3.5
We're utilizing the WebEx Xml API. Here's a sample Xml response from their web service that I'm trying to deserialize into .NET classes.
<?xml version="1.0" encoding="UTF-8"?>
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common"
xmlns:event="http://www.webex.com/schemas/2002/06/service/event"><serv:header><serv:response><serv:result>SUCCESS</serv:result><serv:gsbStatus>PRIMARY</s
erv:gsbStatus></serv:response></serv:header>
<serv:body>
<serv:bodyContent xsi:type="event:lstsummaryEventResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<event:matchingRecords>
<serv:total>2</serv:total>
<serv:returned>2</serv:returned>
<serv:startFrom>1</serv:startFrom>
</event:matchingRecords>
<event:event>
<event:sessionKey>999999</event:sessionKey>
<event:sessionName>Test Event 1</event:sessionName>
<event:sessionType>129</event:sessionType>
<event:hostWebExID>SomeName</event:hostWebExID>
<event:startDate>03/28/2012 14:30:00</event:startDate>
<event:endDate>03/28/2012 14:45:00</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>15</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
</serv:bodyContent>
</serv:body>
</serv:message>
Here's the class that we're deserializing into:
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Collections.Generic;
namespace Masonite.MTier.WebEx
{
[Serializable()]
[XmlRoot("message", Namespace = "http://www.webex.com/schemas/2002/06/service")]
public class lstsummaryEventResponsexx
{
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
public lstsummaryEventResponsexx()
{
xmlns.Add("serv", "http://www.webex.com/schemas/2002/06/service");
xmlns.Add("com", "http://www.webex.com/schemas/2002/06/common");
xmlns.Add("event", "http://www.webex.com/schemas/2002/06/service/event");
}
[XmlElement(ElementName = "header")]
public Header header { get; set; }
[XmlElement(ElementName = "body")]
public Body body { get; set; }
[Serializable()]
[XmlRoot("header")]
public class Header
{
[XmlElement(ElementName = "response")]
public Response response { get; set; }
}
[Serializable()]
[XmlRoot("body")]
[XmlInclude(typeof(lstsummaryEventResponse))]
public class Body
{
[XmlElement(ElementName = "bodyContent", Form = XmlSchemaForm.Qualified)]
public BodyContent bodyContent { get; set; }
}
[Serializable()]
public class lstsummaryEventResponse
{
}
[Serializable()]
[XmlRoot("response")]
public class Response
{
[XmlElement(ElementName = "result")]
public string result { get; set; }
[XmlElement(ElementName = "reason")]
public string reason { get; set; }
[XmlElement(ElementName = "gsbStatus")]
public string gsbStatus { get; set; }
[XmlElement(ElementName = "exceptionID")]
public string exceptionID { get; set; }
}
[Serializable()]
[XmlRoot("bodyContent")]
public class BodyContent
{
[XmlElement(ElementName = "matchingRecords", Namespace = "http://www.webex.com/schemas/2002/06/service/event")]
public MatchingRecords matchingRecords { get; set; }
[XmlElement(ElementName = "event", Namespace = "http://www.webex.com/schemas/2002/06/service/event")]
public List<EventSummary> events { get; set; }
}
[Serializable()]
[XmlRoot("matchingRecords")]
public class MatchingRecords
{
[XmlElement(ElementName = "total", Namespace = "http://www.webex.com/schemas/2002/06/service")]
public int total { get; set; }
[XmlElement(ElementName = "returned", Namespace = "http://www.webex.com/schemas/2002/06/service")]
public int returned { get; set; }
[XmlElement(ElementName = "startFrom", Namespace = "http://www.webex.com/schemas/2002/06/service")]
public int startFrom { get; set; }
}
[Serializable()]
[XmlRoot("event")]
public class EventSummary
{
[XmlElement(ElementName = "sessionKey")]
public long sessionKey { get; set; }
[XmlElement(ElementName = "sessionName")]
public string sessionName { get; set; }
[XmlElement(ElementName = "sessionType")]
public int sessionType { get; set; }
[XmlElement(ElementName = "hostWebExID")]
public string hostWebExID { get; set; }
[XmlElement(ElementName = "startDate")]
public string startDate { get; set; }
[XmlElement(ElementName = "endDate")]
public string endDate { get; set; }
[XmlElement(ElementName = "timeZoneID")]
public int timeZoneID { get; set; }
[XmlElement(ElementName = "duration")]
public int duration { get; set; }
[XmlElement(ElementName = "description")]
public string description { get; set; }
[XmlElement(ElementName = "status")]
public string status { get; set; }
[XmlElement(ElementName = "panelists")]
public string panelists { get; set; }
[XmlElement(ElementName = "listStatus")]
public listingType listStatus { get; set; }
}
}
}
The error I'm receiving:
The specified type was not recognized: name='lstsummaryEventResponse', namespace='http://www.webex.com/schemas/2002/06/service/event', at <bodyContent xmlns='http://www.webex.com/schemas/2002/06/service'>
I'm not sure how to provide the type lstsummaryEventResponse for the Deserialize method. I added another serializable class to my class above using that name, but get the same error. Any thoughts?
BodyContent can have the type event:lstsummaryEventResponse - so you have to declare the corresponding class, and then decorate the declaration of BodyContent as follows:
[Serializable()]
[XmlRoot("bodyContent")]
[XmlInclude("lstsummaryEventResponse")]
public class BodyContent {
}
Having said that, creating C# class with a serialization corresponding to some arbitrary XML is pretty tricky, I am not sure it is right approach