Deserializing XML to object when they are not "shaped" the same? - c#

I have an XML snippet like so:
<coverageCd>WEL
<descriptorCd>SMIO
<descriptorCdStartDate>01/01/2015</descriptorCdStartDate>
<descriptorCdEndDate>12/31/9999</descriptorCdEndDate>
</descriptorCd>
<descriptorCd>AAE
<descriptorCdStartDate>01/01/2015</descriptorCdStartDate>
<descriptorCdEndDate>12/31/9999</descriptorCdEndDate>
</descriptorCd>
</coverageCd>
I need to automagically translate this into the following class structure:
public class XmlCoverageCode
{
public string CoverageCode { get; set; }
public IEnumerable<XmlDescriptor> Descriptors { get; set; }
}
public class XmlDescriptor
{
public string DescriptorCode { get; set; }
public string DescriptorCodeStartDate { get; set; }
public string DescriptorCodeEndDate { get; set; }
}
...so the above XML snippet would translate to this:
var coverageCd = new XmlCoverageCode
{
CoverageCode = "WEL",
Descriptors =
new List<XmlDescriptor>
{
new XmlDescriptor
{
DescriptorCode = "SMIO",
DescriptorCodeStartDate = "01/01/2015",
DescriptorCodeEndDate = "12/31/9999"
},
new XmlDescriptor
{
DescriptorCode = "AAE",
DescriptorCodeStartDate = "01/01/2015",
DescriptorCodeEndDate = "12/31/9999"
}
}
};
Naturally, I would prefer to use built-in mechanisms for doing this. I just don't know if that's even possible.

To get classes from the XML, you can just copy your XML into clipboard and make Edit -> Paste Special -> Paste XML As Classes in Visual Studio. Then, after cleaning up of generated code, we get the following:
[XmlRoot(ElementName = "coverageCd")]
public partial class XmlCoverageCode
{
[XmlText]
public string CoverageCode { get; set; }
[XmlElement("descriptorCd")]
public List<XmlDescriptor> Descriptors { get; set; }
}
public partial class XmlDescriptor
{
[XmlText]
public string DescriptorCode { get; set; }
[XmlElement("descriptorCdStartDate")]
public string DescriptorCodeStartDate { get; set; }
[XmlElement("descriptorCdEndDate")]
public string DescriptorCodeEndDate { get; set; }
}
This is actually the same, as you wrote in the question, but with the required attributes and changed IEnumerable to List, because XmlSerializer doesn't support the first one.
And the code snippet how to serialize/deserialize:
var serializer = new XmlSerializer(typeof(XmlCoverageCode));
var coverageCode = (XmlCoverageCode)serializer.Deserialize(xmlFileStream);
serializer.Serialize(xmlFileStream, coverageCode);

You can use xsd command for converting xml to c# class
Save the xml in the file test.xml
Run xsd.exe test.xml to generate a schema e.g. test.xsd
Run xsd.exe /c test.xsd to generate classes from the schema

Related

c# deserializate xml embedded node to class property

i have part of xml document
<Tender SubTenderType="BC" TenderType="Check">
<TenderTotal>
<Amount>10.00</Amount>
</TenderTotal>
</Tender>
i need convert it to class.
public class Tender
{
public string SubTenderType { get; set; }
public string TenderType { get; set; }
public decimal Amount { get; set; }
}
what i already wrote and this work. but i interseing can i deserialize xml to class as written above?
[Serializable]
public class Tender
{
[XmlAttribute("SubTenderType")]
public string SubTenderType { get; set; }
[XmlAttribute("TenderType")]
public string TenderType { get; set; }
[XmlElement("TenderTotal")]
public TenderTotal TenderTotal { get; set; }
}
[Serializable]
public class TenderTotal
{
[XmlElement("Amount")]
public decimal Amount { get; set; }
}
You can deserialize xml to first Type "Tender" and next use autoMapper to map your type (create new object of different type)
create map:
config.CreateMap<TenderFirst, TenderSecond>().ForMember(x => x.TenderTotal.Amount, y => y.Amount ())
Having the following class without XmlAttribute:
public class Tender
{
public string SubTenderType { get; set; }
public string TenderType { get; set; }
public decimal Amount { get; set; }
}
You can use the XmlAttributeOverrides class to override the behavior of the serializer in such a way that instead of elements it would do the attributes.
var attrSTT = new XmlAttributes { XmlAttribute = new XmlAttributeAttribute("SubTenderType") };
var attrTT = new XmlAttributes { XmlAttribute = new XmlAttributeAttribute("TenderType") };
var overrides = new XmlAttributeOverrides();
overrides.Add(typeof(Tender), nameof(Tender.SubTenderType), attrSTT);
overrides.Add(typeof(Tender), nameof(Tender.TenderType), attrTT);
var xs = new XmlSerializer(typeof(Tender), overrides);
However, in this way impossible add a new item or wrap one element in another.
Therefore, you have to do custom serialization, or map one type to another, or writing a custom xml reader/writer, or perform the read/write manually (for example, using linq2xml). There are many ways...

XML Parsing via XmlReader: Read XMLElement Name as String

I have run into a problem while trying to parse the response of a WebDAV application.
The relevant part of the response looks like this:
for collections:
....
<D:getlastmodified xmlns:B="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" B:dt="dateTime.rfc1123">Tue, 15 Jan 2013 15:47:30 GMT</D:getlastmodified>
<D:displayname>aaa.bc</D:displayname>
<D:resourcetype>
<D:collection />
</D:resourcetype>
<D:getcontenttype>text/html; charset=utf-8</D:getcontenttype>
....
for normal files:
....
<D:getlastmodified xmlns:B="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" B:dt="dateTime.rfc1123">Tue, 15 Jan 2013 15:47:30 GMT</D:getlastmodified>
<D:displayname>aaa.bc</D:displayname>
<D:resourcetype />
<D:getcontenttype>text/html; charset=utf-8</D:getcontenttype>
....
I want to parse this into a c# object with the property:
[XmlElement("resourcetype")]
public string Type {get;set;}
Where e.g. Type = "collection" for a collection.
How would I do this? For the part I posted my C# code looks like this (but does not do what I want):
[XmlRoot("prop")]
public class Prop
{
[XmlElement("creationdate")]
public string CreationDate { get; set; }
[XmlElement("getlastmodified")]
public string LastModified { get; set; }
[XmlElement("displayname")]
public string DisplayName { get; set; }
[XmlElement("resourcetype")]
public string ResourceType { get; set; }
[XmlElement("getcontenttype")]
public string ContentType { get; set; }
[XmlElement("getcontentlength")]
public string ContentLength { get; set; }
[XmlElement("getetag")]
public string ETag { get; set; }
[XmlElement("imagewidth")]
public string ImageWidth { get; set; }
[XmlElement("imageheight")]
public string ImageHeight { get; set; }
[XmlElement("thumbnailuri")]
public string TumbnailUri { get; set; }
}
[XmlRoot("resourcetype")]
public class ResourceType
{
[XmlElement("collection")] // TODO
public string Collection { get; set; }
}
and the method to parse everything:
private T ParseWebDavXml<T>(string xml)
{
using (var reader = XmlReader.Create(new StringReader(xml)))
{
var serializer = new XmlSerializer(typeof(T), "DAV:");
var result = (T)serializer.Deserialize(reader);
return result;
}
}
Check this Link: I have tested this code
Remove the FirtsColumn, SecondColumn from the code and utilize the xmlDS as per your requirements
XML Parsing
You can change the string type to an object type for the ResourceType fied. You also need to decorate ResourceType fied with XmlType attributes. You specify on XmlType attribute by target (in your case, 2).
You also need to create two types :
One for the string,
One for the collection.
But you can use two more simpler solutions :
generate a XSD and the C# classes corresponding to this XSD : see [https://stackoverflow.com/questions/19359691/diff-between-top-1-1-and-select-1-in-sql-select-query/19359757#19359757],
directly define a table, instead of a string field

deserialising does not work

This is the xml stream:
<?xml version="1.0" encoding="utf-8" ?>
<historydetails>
<taskEvent>
<eventtype>Transitions</eventtype>
<historyevent>Task moved</historyevent>
<details>From 'Requested' to 'In Validation'</details>
<author>NAme</author>
<entrydate>01 Jul 13, 11:34</entrydate>
<historyid>2620</historyid>
</taskEvent>
<taskEvent>
<eventtype>Updates</eventtype>
<historyevent>Subtask marked done</historyevent>
<details>Subtask: visualise status and versions</details>
<author>NAme2</author>
<entrydate>21 Jun 13, 10:16</entrydate>
<historyid>2588</historyid>
</taskEvent>
</historydetails>
The corresponding classes look like this:
public class historydetails
{
[XmlElement("taskEvent")]
List<taskEvent> eventList = new List<taskEvent>();
}
public class taskEvent
{
string eventtype { get; set; }
string historyevent { get; set; }
string details { get; set; }
string author { get; set; }
string entrydate { get; set; }
string historyid { get; set; }
}
the code to deserialise the xml (the string replacement contains the xml code):
XmlSerializer deserializer = new XmlSerializer(typeof(historydetails));
object obj = obj = deserializer.Deserialize(stringToStream(replacement));
historydetails XmlData = (historydetails)obj;
The method stringToStream
private MemoryStream stringToStream(string input)
{
byte[] byteArray = Encoding.ASCII.GetBytes(input);
MemoryStream stream = new MemoryStream(byteArray);
return stream;
}
The result that i get is as following:
The object XmlData is made and there is a list of taskEvents.
The problem is in the list itself: it is empty...
You have to make the members public
public class historydetails
{
[XmlElement("taskEvent")]
public List<taskEvent> eventList = new List<taskEvent>();
}
public class taskEvent
{
public string eventtype { get; set; }
public string historyevent { get; set; }
public string details { get; set; }
public string author { get; set; }
public string entrydate { get; set; }
public string historyid { get; set; }
}
As an aside, for future reference (with Visual Studio 2012 or the WebEssentials plugin), one of the easiest way to create the classes based on some sample XML content data is to copy it into the clipboard and then use the built-in VS function: EDIT - > Paste Special -> Paste XML As Classes into a new class file.
It leaves less space for errors like the one you encountered and
It's fast, you'll have your classes ready in a few seconds

XML parsing using attributes

I have XML-based config for application shortcuts bindings. i need to parse it.
<ShortcutBinding>
<ShortcutHandler Name ="Retail.Application.Documents.Outcome.Presentation.OutcomePresenter">
<Shortcut Name="EditHeader">
<Key>CTRL</Key>
<Key>F4</Key>
</Shortcut>
<Shortcut Name="EditItem">
<Key>F4</Key>
</Shortcut>
</ShortcutHandler>
</ShortcutBinding>
I know that .Net has attributes for deserializing XML into objects.
Can anyone write complete example for such deserialization, using attributes.
public class ShortcutBinding
{
public ShortcutHandler ShortcutHandler { get; set; }
}
public class ShortcutHandler
{
[XmlAttribute]
public string Name { get; set; }
[XmlElement("Shortcut")]
public List<Shortcut> Shortcuts { get; set; }
}
public class Shortcut
{
[XmlAttribute]
public string Name { get; set; }
[XmlElement("Key")]
public List<string> Keys { get; set; }
}
Deserializing:
XmlSerializer serializer = new XmlSerializer(typeof(ShortcutBinding));
var binding = (ShortcutBinding)serializer.Deserialize(XmlReader.Create(path));

class design: How do I create a class with nested objects?

I am currently developing a client library for connecting to Newegg using the documentation provided by Newegg and have a question on class design.
In working with various API's ( namely NetSuite and Amazon's MWS ) I come across classes that have are used like this:
recToFulfill.packageList = new ItemFulfillmentPackageList();
recToFulfill.packageList.package = new ItemFulfillmentPackage[ifitemlist.item.Length];
recToFulfill.packageList.package[i] = new ItemFulfillmentPackage();
recToFulfill.packageList.package[i].packageWeightSpecified = true;
recToFulfill.packageList.package[i].packageTrackingNumber = "trackingNumber";
The question I have is: How do I properly design the nested objects like above? I have never had to worry about this previously, so I am unsure on where to look, or start.
The bit I need to figure out looks like this ( taken from the API documentation provided):
<UpdateOrderStatusInfo>
<IsSuccess></IsSuccess>
<Result>
<OrderNumber></OrderNumber>
<SellerID></SellerID>
<OrderStatus></OrderStatus>
</Result>
</UpdateOrderStatusInfo>
All fields are type string, except order number which is an integer.
I have this currently:
public class UpdateOrderStatusInfo
{
public string IsSuccess { get; set; }
public int OrderNumber { get; set; }
public string SellerID { get; set; }
public string OrderStatus { get; set; }
}
But the returned XML Response has Results as a parent node which to me seems like it should be represented within the class itself. Would I just do this?
public UpdateOrderStatusInfo results {get; set;}
If so, where do the child nodes go?
What I need is to be able to say is something like:
UpdateOrderStatusInfo updateInfo = new UpdateOrderStatusInfo();
if(updateInfo.IsSuccess.Equals("true")
{
Console.WriteLine(updateInfo.Results.OrderStatus);
}
Any help, or advice on where to get this information is appreciated.
Easy breezy. If it has no children, it's a scalar property. If it does, it is its own class, and referenced in the parent class accordingly. If it repeats, it's a collection, and is referenced like a class (these are complex type, not primitives). Make sure you initialize them in your constructors).
class Program
{
static void Main(string[] args)
{
var myOrder = new UpdateOrderStatusInfo();
myOrder.IsSuccess = "true";
myOrder.OrderResult.OrderNumber = 1001;
myOrder.OrderResult.OrderStatus = "Pending";
myOrder.OrderResult.SellerID = "69";
}
}
public class UpdateOrderStatusInfo
{
public string IsSuccess { get; set; }
public Result OrderResult { get; set; }
public UpdateOrderStatusInfo()
{
OrderResult = new Result();
}
}
public class Result
{
public int OrderNumber { get; set; }
public string SellerID { get; set; }
public string OrderStatus { get; set; }
}
You need to define the Result as a separate class, called whatever you want, then add a Result property as that type. The Result class can be defined at the namespace level, or, if you are unlikely to use it anywhere else on its own, you can nest the class definition inside the UpdateOrderStatusInfo class:
public class UpdateOrderStatusInfo
{
public class UpdateOrderResult
{
public int OrderNumber { get; set; }
public string SellerID { get; set; }
public string OrderStatus { get; set; }
}
public UpdateOrderStatusInfo()
{
Result = new UpdateOrderResult();
}
public string IsSuccess { get; set; }
public UpdateOrderResult Result { get; set; }
}
The easy way is to use the xsd.exe tool.
The command xsd response.xml will generate the file response.xsd
The command xsd response.xsd /C will generate the file response.cs which contains the classes necessary to serialize/deserialize the xml posted.

Categories