I am trying to serialize a C# object into XML so that it could be used as the body of API call. They are very particular about the input they need. I have built the following class to hold the data I need to send over to them. Including attributes and all properties as Elements instead of attributes. They also require that lists include the type="array" I though that creating my own class the implements a List would be the easiest since all lists I give them must have the same attribute. When serialization occurs it serializes the base class of List items but it doesn't include the attribute I want from the derived class.
public class CustomArray<T> : List<T>
{
[XmlAttribute]
public string type { get; set; } = "array";
}
[XmlRoot("message")]
public class MessageBody
{
[XmlArray("Checks"), XmlArrayItem("CheckItem")]
public CustomArray<Check> CheckList { get; set; }
}
public class Check
{
[XmlElement("C_CHECK_NUMBER")]
public string CheckNumber { get; set; }
[XmlElement("C_CHECK_AMOUNT")]
public decimal Amount { get; set; }
[XmlArray("InvoiceList"), XmlArrayItem("Invoice")]
public CustomArray<Invoice> InvoiceList { get; set; }
}
public class Invoice
{
[XmlElement("C_INVOICE_ID")]
public long ID { get; set; }
[XmlElement("C_INVOICE_NUM")]
public string InvoiceNum { get; set; }
}
I then run this code:
// Create a sample object
var message = new MessageBody()
{
CheckList = new CustomArray<Check>
{
new Check
{
CheckNumber = "111",
Amount = 1.00M
},
new Check
{
CheckNumber = "112",
Amount = 2.00M,
InvoiceList = new CustomArray<Invoice>
{
new Invoice
{
ID = 1,
InvoiceNum = "1"
}
}
}
}
};
// Create custom settings
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true
};
// Serialize item and print it to console
using (var sw = new StringWriter())
using (var writer = XmlWriter.Create(sw, settings))
{
var serializer = new XmlSerializer(message.GetType());
serializer.Serialize(writer, message, new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }));
Console.WriteLine(sw.ToString());
}
I get this written to the console:
<message>
<Checks>
<CheckItem>
<C_CHECK_NUMBER>111</C_CHECK_NUMBER>
<C_CHECK_AMOUNT>1.00</C_CHECK_AMOUNT>
</CheckItem>
<CheckItem>
<C_CHECK_NUMBER>112</C_CHECK_NUMBER>
<C_CHECK_AMOUNT>2.00</C_CHECK_AMOUNT>
<InvoiceList>
<Invoice>
<C_INVOICE_ID>1</C_INVOICE_ID>
<C_INVOICE_NUM>1</C_INVOICE_NUM>
</Invoice>
</InvoiceList>
</CheckItem>
</Checks>
</message>
But I need to get this:
<message>
<Checks type="array">
<CheckItem>
<C_CHECK_NUMBER>111</C_CHECK_NUMBER>
<C_CHECK_AMOUNT>1.00</C_CHECK_AMOUNT>
</CheckItem>
<CheckItem>
<C_CHECK_NUMBER>112</C_CHECK_NUMBER>
<C_CHECK_AMOUNT>2.00</C_CHECK_AMOUNT>
<InvoiceList type="array">
<Invoice>
<C_INVOICE_ID>1</C_INVOICE_ID>
<C_INVOICE_NUM>1</C_INVOICE_NUM>
</Invoice>
</InvoiceList>
</CheckItem>
</Checks>
</message>
Thank you for your help!
Here is a dotnetfiddle that I made to show it off. It's not exact but it has the same idea. https://dotnetfiddle.net/ALCX5H
Try following :
[XmlRoot("message")]
public class MessageBody
{
[XmlElement("Checks")]
public Checks Checks { get; set; }
}
public class Checks
{
[XmlAttribute]
public string type { get; set; }
[XmlElement("Checks")]
public List<Check> Checks { get; set; }
}
public class Check
{
[XmlElement("C_CHECK_NUMBER")]
public string CheckNumber { get; set; }
[XmlElement("C_CHECK_AMOUNT")]
public decimal Amount { get; set; }
}
Related
I need to serialize a class to xml. If a certain condition is met at run-time, I want to add an XML attribute to an element and assign it a value. Sometimes, the "Error" attribute will appear and sometimes it won't.
My code that serializes my objects:
public class XmlToolsRepo : IXmlTools
{
public string SerializeToXML<T>(object obj)
{
string results = null;
Encoding enc = Encoding.UTF8;
using (MemoryStream ms = new MemoryStream())
{
using (XmlTextWriter xw = new XmlTextWriter(ms, enc))
{
xw.Formatting = Formatting.None;
XmlSerializerNamespaces emptyNS = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });
XmlSerializer xSerializer = new XmlSerializer(typeof(T));
xSerializer.Serialize(xw, obj, emptyNS);
}
results = enc.GetString(ms.ToArray());
}
return results;
}
}
A class with a property that could have a new attribute at run-time:
[DataContract]
public class H204
{
[DataMember]
[XmlAttribute]
public string Code { get; set; }
[DataMember]
public string DW { get; set; }
}
When a condition is met I need for the XML to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<H204 Code="A">
<DW Error="test" />
</H204>
Try following :
public class H204
{
[XmlAttribute(AttributeName = "Code")]
public string Code { get; set; }
[XmlElement(ElementName = "DW")]
public DW dw{ get; set; }
}
public class DW
{
[XmlAttribute(AttributeName = "Error")]
public string text { get; set; }
}
I've been looking here for a sollution, but I did not find it in previous answers.
I need to create the following xml with the serialiser
<?xml version="1.0" encoding="Windows-1252"?>
<documents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<document>
<Keys>
<drawer>GraphicData</drawer>
<somedata>otherData</somedata>
</Keys>
<otherGenericProperties>
<Data>GenericData 2</Data>
</otherGenericProperties>
<Repeat>
<FileInfo mimeType="application/pdf" HREF="PdfFile.pdf" />
</Repeat>
<Repeat>
<FileInfo mimeType="application/pdf" HREF="PdfFile2.pdf" />
</Repeat>
</document>
</documents>
The data consist of a few classes
namespace test
{
public class Documents
{
[XmlElement("Keys")] // this is used so the destination name can change in the future
public Keys keys { get; set; }
}
public class document
{
[XmlElement("Keys")] // this is used so the destination name can change in the future
public Keys keys { get; set; }
[XmlElement("Repeat")] // This does not work
List<Repeat> listRepeat { get; set; }
}
public class Keys
{
[XmlElement("drawer")]
public string drawer { get; set; }
}
public class Repeat
{
[XmlElement("fileInfo")]
public FileInfo fileInfo { get; set; }
}
public class FileInfo
{
public string FileInfo { get; set; }
[XmlAttribute("mimeType")]
public string mimeType { get; set; }
[XmlAttribute("mimeType")]
public string HREF { get; set; }
}
}
the serialiser :
XmlSerializer serializer = new XmlSerializer(typeof(Documents));
using (StreamWriter writer = new StreamWriter(saveBestand, false, xmlEncoding))
{
serializer.Serialize(writer, icm, namespaces);
}
I really need the xm like the example, and also the xml names should be changable by a contract like xmlElement is used for. Somehow the repeat element cant be placed on the level you see in the example. Does anyone have a sollution ?
you have two options.
Option 1
use xsd.exe to generate classes using the xml you want.
xsd yourxmlfile.xml
xsd yourxmlfile.xsd /classes
This will generate classes which you can use for serialization or deserialization.
Option 2:
Paste special option to generate the c# classes.
This does not involve using the command line.
refer blog at: https://dennymichael.net/2014/05/30/convert-xml-to-csharp-classes/comment-page-1/
It seems that you poorly designed your data classes. Try solution below.
public class OtherGenericProperties
{
[XmlElement("Data")]
public string Data { get; set; }
}
[XmlRoot("documents")]
public class Documents
{
[XmlElement("document")]
public Document Document { get; set; }
}
public class Document
{
[XmlElement("Keys")] // this is used so the destination name can change in the future
public Keys Keys { get; set; }
[XmlElement("otherGenericProperties")]
public OtherGenericProperties OtherGenericProperties { get; set; }
[XmlElement("Repeat")] // This does not work
public List<Repeat> ListRepeat { get; set; }
}
public class Keys
{
[XmlElement("drawer")]
public string Drawer { get; set; }
[XmlElement("somedata")]
public string SomeData { get; set; }
}
public class Repeat
{
[XmlElement("FileInfo")]
public FileInfo FileInfo { get; set; }
}
public class FileInfo
{
[XmlAttribute("mimeType")]
public string MimeType { get; set; }
[XmlAttribute("HREF")]
public string Href { get; set; }
}
Serializing:
internal static void Test()
{
var doc = new Documents
{
Document = new Document
{
Keys = new Keys
{
Drawer = "GraphicData",
SomeData = "otherData"
},
OtherGenericProperties = new OtherGenericProperties { Data = "GenericData 2" },
ListRepeat = new List<Repeat>
{
new Repeat { FileInfo =new FileInfo { Href = "PdfFile.pdf", MimeType = "application/pdf" } },
new Repeat { FileInfo = new FileInfo { Href = "PdfFile2.pdf", MimeType = "application/pdf" } }
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Documents));
using (var f = new StreamWriter("D:\\doc.xml", false, Encoding.GetEncoding("Windows-1252")))
{
serializer.Serialize(f, doc);
f.Flush();
}
}
Output:
<?xml version="1.0" encoding="Windows-1252"?>
<documents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<document>
<Keys>
<drawer>GraphicData</drawer>
<somedata>otherData</somedata>
</Keys>
<otherGenericProperties>
<Data>GenericData 2</Data>
</otherGenericProperties>
<Repeat>
<FileInfo mimeType="application/pdf" HREF="PdfFile.pdf" />
</Repeat>
<Repeat>
<FileInfo mimeType="application/pdf" HREF="PdfFile2.pdf" />
</Repeat>
</document>
</documents>
I have to serialize my objects to a XML document in a certain order.
<?xml version="1.0"?>
<__Root __version="1.1" __encryption="2">
<__service __serviceType="Test">
<__inputData>
<BaseToChange>
<__name>TestName</__name>
</BaseToChange>
</__inputData>
<__perform>
<__eventName>Event</__eventName>
</__perform>
<__inputData>
<ObjectChanges>
<Name>Test</Name>
</ObjectChanges>
</__inputData>
<__execute />
<__requestData>
<CompletionMsg />
</__requestData>
</__service>
</__Root>
The problem I have now is that I'm not able to serialize my List<InputData> with the Element Perform in between.
public class Service
{
[XmlAttribute("__serviceType")]
public string ServiceType { get; set; }
[XmlElement("__perform")]
public Perform Perform { get; set; }
[XmlElement("__inputData")]
public List<InputData> InputData{ get; set; }
[XmlElement("__execute")]
public Execute Execute { get; set; }
[XmlElement("__requestData")]
public RequestData RequestData{ get; set; }
public Service() { }
}
The order has to be as shown. So first <__inputData>, then <__perform>, followed by any remaining <__inputData>.
I already tried to separate the Properties and therefore the XmlElements but as soon I want to serialize with two elements having the same name I get an error.
Does anyone have an idea how to accomplish that?
You can take advantage of the polymorphic element functionality [XmlElement(Type = typeof(TElement))] to create a surrogate object array that contains both the Perform and InputData objects, in the correct sequence:
public class Service
{
[XmlAttribute("__serviceType")]
public string ServiceType { get; set; }
[XmlIgnore]
public Perform Perform { get; set; }
[XmlIgnore]
public List<InputData> InputData { get; set; }
[XmlElement("__perform", Type = typeof(Perform))]
[XmlElement("__inputData", Type = typeof(InputData))]
public object[] XmlInputAndPerformance
{
get
{
var inputData = (InputData ?? Enumerable.Empty<InputData>()).Cast<object>();
var performData = Perform == null ? Enumerable.Empty<object>() : new object[] { Perform };
return inputData.Take(1)
.Concat(performData)
.Concat(inputData.Skip(1))
.ToArray();
}
set
{
if (value == null)
return;
var newInputs = value.OfType<InputData>().ToList();
var newPerform = value.OfType<Perform>().ToList();
if (newInputs.Count + newPerform.Count != value.Length)
throw new ArgumentException("Unknown type.");
if (newPerform.Count > 1)
throw new ArgumentException("Too many Perform objects.");
if (newPerform.Count > 0)
Perform = newPerform[0];
(InputData = InputData ?? new List<InputData>()).AddRange(newInputs);
}
}
[XmlElement("__execute")]
public Execute Execute { get; set; }
[XmlElement("__requestData")]
public RequestData RequestData { get; set; }
public Service() { }
}
Note that the original InputData and Perform properties have been marked with [XmlIgnore] and that two [XmlElement(name, Type = typeof(TElement))] attributes have been added, one for InputData and one for Perform.
Sample fiddle.
The XML below always comes in this format, but the elements under the <Hit> node are dynamic, the name or number of items could be different each time. Is it possible to get the elements under the <Hit> node dynamically.
<SearchResponse>
<Response>
<Qtime>3</Qtime>
<HitsPerPage>10</HitsPerPage>
</Response>
<HitsCount>
<total>33</total>
<start>0</start>
<end>10</end>
</HitsCount>
<Hits>
<Hit>
<id type=''>123</id>
<eid type=''>456</eid>
<Title type='t'>
<![CDATA[title goes here]]>
</Title>
</Hit>
<Hit>
<id type=''>123</id>
<oid type=''>456</oid>
<Title type='t'>
<![CDATA[title goes here]]>
</Title>
<Description type='s'>
<![CDATA[Description goes here]]>
</Description>
</Hit>
</Hits>
</SearchResponse>
Edit: here's the C# code, it's working fine and get the id from the <Hit> node since I already defined the property, but I need to get all of them dynamic.
[XmlRoot("SearchResponse")]
public sealed class SearchResponse {
[XmlElement("Response", Type = typeof(Response))]
public Response[] Responses { get; set; }
[XmlElement("HitsCount", Type = typeof(HitsCount))]
public HitsCount[] HitsCount { get; set; }
[XmlElement("Hits", Type = typeof(Hits))]
public Hits[] Hits { get; set; }
public static SearchResponse GetSearchResponseObject(string xmlString) {
try {
var reader = new StringReader(xmlString);
var serializer = new XmlSerializer(typeof(SearchResponse));
var instance = (SearchResponse)serializer.Deserialize(reader);
return instance;
} catch (Exception ex) {
var asd = ex.Message;
return null;
}
}
}
[Serializable]
public class Response {
[XmlElement("Qtime")]
public string Qtime { get; set; }
[XmlElement("HitsPerPage")]
public string HitsPerPage { get; set; }
}
[Serializable]
public class HitsCount {
[XmlElement("total")]
public string Total { get; set; }
[XmlElement("start")]
public string Start { get; set; }
[XmlElement("end")]
public string End { get; set; }
}
[Serializable]
public class Hits {
[XmlElement("Hit")]
public Hit[] Hit { get; set; }
}
[Serializable]
public class Hit {
[XmlElement("id")]
public string Id { get; set; }
}
Edit 2: // Hit class code
public class Hit {
// Since "id" is expected in the XML, deserialize it explicitly.
[XmlElement("id")]
public string Id { get; set; }
private readonly List<XElement> _elements = new List<XElement>();
[XmlAnyElement]
public List<XElement> Elements { get { return _elements; } }
}
Since you don't know what elements might be present in your Hit class, you can add a List<XElement> property to you class and attach the [XmlAnyElement] attribute to it. It will then capture any and all unknown elements in the XML for the class. Once the elements are deserialized, you can add API properties to query for elements with specific names, for instance:
public class Hit
{
// Since "id" is expected in the XML, deserialize it explicitly.
[XmlElement("id")]
public string Id { get; set; }
private readonly List<XElement> elements = new List<XElement>();
[XmlAnyElement]
public List<XElement> Elements { get { return elements; } }
#region convenience methods
public string this[XName name]
{
get
{
return Elements.Where(e => e.Name == name).Select(e => e.Value).FirstOrDefault();
}
set
{
var element = Elements.Where(e => e.Name == name).FirstOrDefault();
if (element == null)
Elements.Add(element = new XElement(name));
element.Value = value;
}
}
const string title = "Title";
[XmlIgnore]
public string Title
{
get
{
return this[title];
}
set
{
this[title] = value;
}
}
#endregion
}
Incidentally, you can eliminate your Hits class if you mark the Hits array with [XmlArray] rather than [XmlElement], like so:
[XmlRoot("SearchResponse")]
public sealed class SearchResponse
{
[XmlElement("Response", Type = typeof(Response))]
public Response[] Responses { get; set; }
[XmlElement("HitsCount", Type = typeof(HitsCount))]
public HitsCount[] HitsCount { get; set; }
[XmlArray("Hits")] // Indicates that the hits will be serialized with an outer container element named "Hits".
[XmlArrayItem("Hit")] // Indicates that each inner entry element will be named "Hit".
public Hit [] Hits { get; set; }
}
Update
The
public string this[XName name] { get; set; }
Is an indexer. See Using Indexers (C# Programming Guide). I added it so it would be easy to do things like:
var description = hit["Description"];
var title = hit["Title"];
The indexer looks for the first XML element with the specified name, and returns its text value. If you don't want it, you can leave it out -- it's just for convenience.
I'm trying to deserialize a reponse from a REST API.
"<FieldListDTO xmlns=\"api.playcento.com/1.0\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
<Allfield>
<FieldDTO>
<Fieldname>Mobile nr</Fieldname>
<Fieldtype>T</Fieldtype>
<Fieldvalue>003241234578</Fieldvalue>
<Fk_id_page>CP584ea74ce5ad4e2d8561d75fc6944f96</Fk_id_page>
<Id_field>FI152dcde5ef9849898b12d6a3f2cdb4ee</Id_field>
<Required>true</Required>
</FieldDTO>
</Allfield>
<Totalcount>1</Totalcount>
</FieldListDTO>"
The Field class:
namespace PlaycentoAPI.Model
{
[XmlRoot("FieldListDTO",Namespace = "api.playcento.com/1.0")]
[XmlType("FieldListDTO")]
public class FieldListDTO
{
public FieldListDTO() { }
[XmlElement("Totalcount")]
public int TotalCount { get; set; }
[XmlArray("Allfield")]
[XmlArrayItem("FieldDTO", typeof(Field))]
public Field[] Field { get; set; }
}
[XmlRoot("FieldDTO", Namespace = "api.paycento.com/1.0")]
[XmlType("FieldDTO")]
public class Field
{
public Field()
{
}
[XmlElement("Id_field")]
public string ID_Field { get; set; }
[XmlElement("Fieldtype")]
public string FieldType { get; set; }
[XmlElement("Fk_id_page")]
public string FK_ID_PAGE { get; set; }
[XmlElement("Required")]
public bool Required { get; set; }
[XmlElement("Fieldname")]
public string FieldName { get; set; }
[XmlElement("Fieldvalue")]
public string FieldValue { get; set; }
}
}
My code which calls the API and deserializes it:
string response = Helper.PerformAndReadHttpRequest(uri, "GET", "");
FieldListDTO myObject;
XmlReaderSettings settings = new XmlReaderSettings();
using (StringReader textReader = new StringReader(response))
{
using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
{
XmlSerializer mySerializer = new XmlSerializer(typeof(FieldListDTO));
myObject = (FieldListDTO)mySerializer.Deserialize(xmlReader);
}
}
return myObject.Field;
In my actual response, I'm getting back 14 FieldDTO's. After deserializing the xml, FieldListDTO myObject contains TotalCount = 14 and Field is an array containing 14 Field's. But all the properties of these fields are NULL (or false).
I'm using the same method for several other API calls. I've compared the classes and the only difference that I see is that the class (Field) has an bool property. So I thought that was the problem. I've changed the bool property to a string but still all the properties were NULL after deserialization.
First thing to catch my eye is that the namespace in your FieldDTO class doesn't match the one in the XML document.
"api.paycento.com/1.0"
"api.playcento.com/1.0"