I want to desirialize an XML file to C# Objects. My objects are as follows
[Serializable]
[XmlRoot(ElementName = "Collection")]
public class Collection
{
public Collection()
{
Artiesten = new List<Artiest>();
Albums = new List<Album>();
Nummers = new List<Nummer>();
}
[XmlElement("Artiesten")]
public List<Artiest> Artiesten { get; set; }
[XmlElement("Albums")]
public List<Album> Albums { get; set; }
[XmlElement("Nummers")]
public List<Nummer> Nummers { get; set; }
}
[Serializable]
public class Artiest
{
[XmlAttribute("artiestid")]
public int ArtiestId { get; set; }
[XmlElement(ElementName = "Naam")]
public String Naam { get; set; }
[XmlElement(ElementName = "Albums")]
public List<Album> Albums { get; set; }
}
[Serializable]
public class Nummer
{
[XmlAttribute("nummerid")]
public int NummerId { get; set; }
[XmlElement(ElementName = "titel")]
public String Titel { get; set; }
[XmlElement(ElementName = "duur")]
public String Duration { get; set; }
}
My XML is this:
<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Artiesten>
<Artiest artiestid="1">
<Naam>Harry</Naam>
<Albums>
<Album albumid="1">
<Titel>Album1</Titel>
<prijs valuta="Euro">19.99</prijs>
<uitgiftejaar>1999</uitgiftejaar>
<Nummers>
<Nummer nummerid="1">
<titel>happy Sundays</titel>
<duur>PT02M02S</duur>
</Nummer>
</Nummers>
</Album>
</Albums>
</Artiest>
</Artiesten>
<Albums>
<Album albumid="1">
<Titel>Album1</Titel>
<prijs valuta="Euro">19.99</prijs>
<uitgiftejaar>1999</uitgiftejaar>
<Nummers>
<Nummer nummerid="1">
<titel>Happy Sundays</titel>
<duur>PT02M02S</duur>
</Nummer>
</Nummers>
</Album>
</Albums>
<Nummers>
<Nummer nummerid="1">
<titel>Happy Sundays</titel>
<duur>PT02M02S</duur>
</Nummer>
</Nummers>
</Collection>
And I'm trying to desirialize like this:
XDocument doc = XDocument.Load(file);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Collection));
using (var reader = doc.Root.CreateReader())
{
Collection collection = (Collection) xmlSerializer.Deserialize(reader);
}
For some reason I can't find the lists in the Collection object are all empty. Debugging shows that the loaded file in XDocument is valid.
EDIT: I managed to narrow down the problem. It does deserialize the lists correctly, only all property's of the Objects in those lists are empty.
Found my answer
I had to edit My list Attributes to this:
[XmlElement("Artiesten", typeof(List<Artiest>))]
public List<Artiest> Artiesten { get; set; }
[XmlElement("Albums", typeof(List<Album>))]
public List<Album> Albums { get; set; }
[XmlElement("Nummers", typeof(List<Nummer>))]
public List<Nummer> Nummers { get; set; }
Collection collection = null;
string path = "file.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Collection));
StreamReader reader = new StreamReader(path);
collection = (Collection)serializer.Deserialize(reader);
reader.Close();
You need to re move the [XmlElement] tags from your lists. Otherwise it works with a different XML structure.
For example, rather than nest all your Artiest objects within a single Artiesten element (which is what your current XML is), it will actually set them adjacent to each other like this:
<Artiesten artiestid="1">
<Naam>Harry</Naam>
<Albums>
<Album albumid="1">
<Titel>Album1</Titel>
<prijs valuta="Euro">19.99</prijs>
<uitgiftejaar>1999</uitgiftejaar>
<Nummers>
<Nummer nummerid="1">
<titel>happy Sundays</titel>
<duur>PT02M02S</duur>
</Nummer>
</Nummers>
</Album>
</Albums>
</Artiesten>
<Artiesten artiestid="2">
<Naam>Harry</Naam>
<Albums>
<Album albumid="1">
<Titel>Album1</Titel>
<prijs valuta="Euro">19.99</prijs>
<uitgiftejaar>1999</uitgiftejaar>
<Nummers>
<Nummer nummerid="1">
<titel>happy Sundays</titel>
<duur>PT02M02S</duur>
</Nummer>
</Nummers>
</Album>
</Albums>
</Artiesten>
<Artiesten artiestid="3">
<Naam>Harry</Naam>
<Albums>
<Album albumid="1">
<Titel>Album1</Titel>
<prijs valuta="Euro">19.99</prijs>
<uitgiftejaar>1999</uitgiftejaar>
<Nummers>
<Nummer nummerid="1">
<titel>happy Sundays</titel>
<duur>PT02M02S</duur>
</Nummer>
</Nummers>
</Album>
</Albums>
</Artiesten>
So try redefining your classes as such:
[Serializable]
[XmlRoot(ElementName = "Collection")]
public class Collection
{
public Collection()
{
Artiesten = new List<Artiest>();
Albums = new List<Album>();
Nummers = new List<Nummer>();
}
public List<Artiest> Artiesten { get; set; }
public List<Album> Albums { get; set; }
public List<Nummer> Nummers { get; set; }
}
[Serializable]
public class Artiest
{
[XmlAttribute("artiestid")]
public int ArtiestId { get; set; }
[XmlElement(ElementName = "Naam")]
public String Naam { get; set; }
public List<Album> Albums { get; set; }
}
You want XmlArray, not XmlElement:
[XmlArray("Artiesten")]
[XmlArrayItem("Artiest")]
public List<Artiest> ...
Actually this is the default behaviour for lists, so you also just remove the attribute completely.
Related
The output i want as below
<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/' >
<SOAP:Body UserGUID = '{redacted}' >
<m:SaveOrder xmlns:m = 'http://www.e-courier.com/schemas/' >
<Order UserID = '1' Notes = 'Signature Requiered' CustomerID = '3' >
</Order >
</m:SaveOrder >
</SOAP:Body >
</SOAP:Envelope >
The output xml that i am getting as my result
<?xml version="1.0"?>
<SOAP:Envelope xmlns:m="http://www.e-courier.com/schemas/" xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body UserGUID="">
<m:SaveOrder >
<m:Order UserID="" Notes="" CustomerID="" />
</m:SaveOrder>
</SOAP:Body>
</SOAP:Envelope>
My XML Class code:
[XmlRoot(ElementName="Order")]
public class Order {
[XmlAttribute(AttributeName="UserID")]
public string UserID { get; set; }
[XmlAttribute(AttributeName="Notes")]
public string Notes { get; set; }
[XmlAttribute(AttributeName="CustomerID")]
public string CustomerID { get; set; }
}
[XmlRoot(ElementName="SaveOrder", Namespace="http://www.e-courier.com/schemas/")]
public class SaveOrder {
[XmlElement(ElementName="Order")]
public Order Order { get; set; }
[XmlAttribute(AttributeName="m", Namespace="http://www.w3.org/2000/xmlns/")]
public string M { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="SaveOrder", Namespace="http://www.e-courier.com/schemas/")]
public SaveOrder SaveOrder { get; set; }
[XmlAttribute(AttributeName="UserGUID")]
public string UserGUID { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="SOAP", Namespace="http://www.w3.org/2000/xmlns/")]
public string SOAP { get; set; }
}
My Code where i am generating xml
var SaveOrder = new ECSaveOrderRequest.Envelope
{
Body = new ECSaveOrderRequest.Body
{
UserGUID = guid,
SaveOrder = new ECSaveOrderRequest.SaveOrder
{
Order = new ECSaveOrderRequest.Order
{
UserID = Uid,
Notes = "",
CustomerID=""
}
}
}
};
var ns = new XmlSerializerNamespaces();
ns.Add("SOAP", "http://schemas.xmlsoap.org/soap/envelope/");
ns.Add("m", "http://www.e-courier.com/schemas/");
var ser = new XmlSerializer(typeof(ECSaveOrderRequest.Envelope));
using (var ms = new MemoryStream())
{
// write the DTO to the MemoryStream
ser.Serialize(ms, SaveOrder, ns);
using (var wc = new WebClient())
{
wc.Encoding = System.Text.Encoding.UTF8;
ms.Position = 0;
StreamReader stream = new StreamReader(ms);
string requestString = stream.ReadToEnd();
var resp = wc.UploadData(ECUrl, ms.ToArray());
}
}
You need to explicitly clear the xml namespace on SaveOrder.Order or the serializer will default to SaveOrder's xml namespace.
Here you go:
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace ECSaveOrderRequest
{
/*
* <SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/' >
<SOAP:Body UserGUID = '{redacted}' >
<m:SaveOrder xmlns:m = 'http://www.e-courier.com/schemas/' >
<Order UserID = '1' Notes = 'Signature Requiered' CustomerID = '3' >
</Order >
</m:SaveOrder >
</SOAP:Body >
</SOAP:Envelope >*/
public class Order
{
[XmlAttribute(AttributeName = "UserID")]
public string UserID { get; set; }
[XmlAttribute(AttributeName = "Notes")]
public string Notes { get; set; }
[XmlAttribute(AttributeName = "CustomerID")]
public string CustomerID { get; set; }
}
public class SaveOrder
{
[XmlElement(ElementName = "Order", Namespace = "")]
public Order Order { get; set; }
}
public class Body
{
[XmlElement(ElementName = "SaveOrder", Namespace = "http://www.e-courier.com/schemas/")]
public SaveOrder SaveOrder { get; set; }
[XmlAttribute(AttributeName = "UserGUID")]
public string UserGUID { get; set; }
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName = "SOAP", Namespace = "http://www.w3.org/2000/xmlns/")]
public string SOAP { get; set; }
}
class Program
{
static void Main(string[] args)
{
var SaveOrder = new ECSaveOrderRequest.Envelope
{
Body = new ECSaveOrderRequest.Body
{
UserGUID = "{redacted}",
SaveOrder = new ECSaveOrderRequest.SaveOrder
{
Order = new ECSaveOrderRequest.Order
{
UserID = "1",
Notes = "Signature Requiered",
CustomerID = "3"
}
}
}
};
var ns = new XmlSerializerNamespaces();
ns.Add("SOAP", "http://schemas.xmlsoap.org/soap/envelope/");
ns.Add("m", "http://www.e-courier.com/schemas/");
var ser = new XmlSerializer(typeof(ECSaveOrderRequest.Envelope));
var ms = new MemoryStream();
// write the DTO to the MemoryStream
ser.Serialize(ms, SaveOrder, ns);
ms.Position = 0;
var xml = Encoding.UTF8.GetString(ms.GetBuffer());
Console.WriteLine(xml);
Console.ReadKey();
}
}
}
outputs
<?xml version="1.0"?>
<SOAP:Envelope xmlns:m="http://www.e-courier.com/schemas/" xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body UserGUID="{redacted}">
<m:SaveOrder>
<Order UserID="1" Notes="Signature Requiered" CustomerID="3" />
</m:SaveOrder>
</SOAP:Body>
</SOAP:Envelope>
Which is a serialization of the same XML document as
<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/' >
<SOAP:Body UserGUID = '{redacted}' >
<m:SaveOrder xmlns:m = 'http://www.e-courier.com/schemas/' >
<Order UserID = '1' Notes = 'Signature Requiered' CustomerID = '3' >
</Order >
</m:SaveOrder >
</SOAP:Body>
</SOAP:Envelope>
.
Many Thanks in advance for any help in creating a class and deserializing xml file like below in .NET
example of xml data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Article>
<Story>
<Title>Some Title 1</Title>
<Author>John</Author>
<Lead>Some Lead 1</Lead>
<Subtitle>SubTitle 1-1</Subtitle>
<Body>body 1-1-1</Body>
<Body>body 1-1-2</Body>
<Body>body 1-1-3</Body>
<Body>body 1-1-4</Body>
<Subtitle>SubTitle 1-2</Subtitle>
<Body>body 1-2-1</Body>
<Body>body 1-2-2</Body>
<Subtitle>SubTitle 1-3</Subtitle>
<Body>body 1-3-1</Body>
<Body>body 1-3-2</Body>
<Body>body 1-3-3</Body>
</Story>
<Story>
<Title>Some Title 2</Title>
<Author>Adam</Author>
<Lead>Some Lead 2</Lead>
<Subtitle>SubTitle 2-1</Subtitle>
<Body>body 2-1-1</Body>
<Body>body 2-1-2</Body>
<Body>body 2-1-3</Body>
<Subtitle>SubTitle 2-2</Subtitle>
<Body>body 2-2-1</Body>
<Body>body 2-2-2</Body>
<Subtitle>SubTitle 2-3</Subtitle>
<Body>body 2-3-1</Body>
</Story>
<Story>
<Picture>
<Image href="someFile1.jpg"></Image>
<Credit>Credit 1</Credit>
<Description>Description Image 1</Description>
</Picture>
</Story>
<Story>
<Picture>
<Image href="someFile2.jpg"></Image>
<Credit>Credit 2</Credit>
</Picture>
</Story>
</Article>
</Root>
I have prepared Domain class for like this (but maybe it's not the best idea for my xml file)
using System.Collections.Generic;
using System.Xml.Serialization;
namespace QgeImagingXmlConnector.Domain
{
[XmlRoot(ElementName = "Root")]
public class InputXmlModel
{
[XmlElement("Article")]
public List<Article> Articles { get; set; }
}
public class Article
{
[XmlElement("Story")]
public List<Story> Stories { get; set; }
}
public class Story
{
public string Title { get; set; }
public string Author { get; set; }
public string Lead { get; set; }
public List<Item> Items { get; set; }
//OR
public List<StoryPicture> Pictures { get; set; }
}
public class StoryPicture
{
public string ImageHref { get; set; }
public string Credit { get; set; }
public string Description { get; set; }
}
public class Item
{
public string ItemType { get; set; } // Possible: Body or Subtitle
public string ItemText { get; set; }
}
}
and method for deserialize
public InputXmlModel GetInputXmlModelByXmlFile(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(InputXmlModel));
TextReader tr = new StreamReader(filePath);
var result = (InputXmlModel)serializer.Deserialize(tr);
tr.Close();
return result;
}
And my question is: How to change my class to work ( by adding some attributes or change structure )
STORY could by like Story with some content or Story with only picture
so in my class i added 2 classes Story and StoryPicture
In Content of Story we can have many body or subtitle tags - but the order is important
Regards
P.S.
this is what i want to get as an result
I put data only for first Story
var result = new InputXmlModel
{
Articles = new List<Article>
{
{
new Article
{
Stories = new List<Story>
{
{new Story
{
Title = "Some Title 1",
Author = "John",
Lead="Some Lead 1",
Items = new List<Item>
{
new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-1"},
new Item{ItemType = "Body", ItemText = "body 1-1-1"},
new Item{ItemType = "Body", ItemText = "body 1-1-2"},
new Item{ItemType = "Body", ItemText = "body 1-1-3"},
new Item{ItemType = "Body", ItemText = "body 1-1-4"},
new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-2"},
new Item{ItemType = "Body", ItemText = "body 1-2-1"},
new Item{ItemType = "Body", ItemText = "body 1-2-2"},
new Item{ItemType = "Subtitle", ItemText = "SubTitle 1-3"},
new Item{ItemType = "Body", ItemText = "body 1-3-1"},
new Item{ItemType = "Body", ItemText = "body 1-3-2"},
new Item{ItemType = "Body", ItemText = "body 1-3-3"},
}
}
}
// here next 3 stories ( one with Items, two for pictures )
}
}
}
}
};
Yes you can do this too like this.
Your model class would like:
using System.Collections.Generic;
using System.Xml.Serialization;
namespace QgeImagingXmlConnector.Domain
{
[XmlRoot(ElementName = "Root")]
public class InputXmlModel
{
[XmlElement("Article")]
public List<Article> Articles { get; set; }
}
public class Article
{
[XmlElement("Story")]
public List<Story> Stories { get; set; }
}
public class Story
{
public string Title { get; set; }
public string Author { get; set; }
public string Lead { get; set; }
[XmlElement("Item")]
public List<Item> Items { get; set; }
[XmlElement("Picture")]
public List<StoryPicture> Pictures { get; set; }
}
public class StoryPicture
{
public string ImageHref { get; set; }
public string Credit { get; set; }
public string Description { get; set; }
}
public class Item
{
public string ItemType { get; set; } // Possible: Body or Subtitle
public string ItemText { get; set; }
}
}
And your XML like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<Article>
<Story>
<Title>Some Title 1</Title>
<Author>John</Author>
<Lead>Some Lead 1</Lead>
<Subtitle>SubTitle 1-1</Subtitle>
<Body>body 1-1-1</Body>
<Body>body 1-1-2</Body>
<Body>body 1-1-3</Body>
<Body>body 1-1-4</Body>
<Subtitle>SubTitle 1-2</Subtitle>
<Body>body 1-2-1</Body>
<Body>body 1-2-2</Body>
<Subtitle>SubTitle 1-3</Subtitle>
<Body>body 1-3-1</Body>
<Body>body 1-3-2</Body>
<Body>body 1-3-3</Body>
</Story>
<Story>
<Title>Some Title 2</Title>
<Author>Adam</Author>
<Lead>Some Lead 2</Lead>
<Subtitle>SubTitle 2-1</Subtitle>
<Body>body 2-1-1</Body>
<Body>body 2-1-2</Body>
<Body>body 2-1-3</Body>
<Subtitle>SubTitle 2-2</Subtitle>
<Body>body 2-2-1</Body>
<Body>body 2-2-2</Body>
<Subtitle>SubTitle 2-3</Subtitle>
<Body>body 2-3-1</Body>
<Picture>
<Image href="someFile1.jpg"></Image>
<Credit>Credit 1</Credit>
<Description>Description Image 1</Description>
</Picture>
<Picture>
<Image href="someFile2.jpg"></Image>
<Credit>Credit 2</Credit>
</Picture>
</Story>
</Article>
</Root>
Is that what you're looking for?
Try following xml linq :
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication100
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Article article = doc.Descendants("Article").Select(x => new Article() { Stories = x.Elements("Story").Select(y => Story.ParseStory(y)).ToList() }).FirstOrDefault();
}
}
public class InputXmlModel
{
public List<Article> Articles { get; set; }
}
public class Article
{
public List<Story> Stories { get; set; }
}
public class Story
{
public string Title { get; set; }
public string Author { get; set; }
public string Lead { get; set; }
public List<Item> Items { get; set; }
public List<StoryPicture> Pictures { get; set; }
enum State
{
DEFAULT,
SUBTITLE,
}
public static Story ParseStory(XElement xStory)
{
Story story = new Story();
State state = State.DEFAULT;
Item newItem = null;
StoryPicture newPicture = null;
foreach (XElement child in xStory.Elements())
{
switch(state)
{
case State.DEFAULT :
switch (child.Name.LocalName)
{
case "Title" :
story.Title = (string)child;
break;
case "Author":
story.Author = (string)child;
break;
case "Lead":
story.Lead = (string)child;
break;
case "Subtitle":
newItem = new Item();
if (story.Items == null) story.Items = new List<Item>();
story.Items.Add(newItem);
state = State.SUBTITLE;
break;
case "Picture":
newPicture = new StoryPicture()
{
ImageHref = (string)child.Element("Image").Attribute("href"),
Credit = (string)child.Element("Credit"),
Description = (string)child.Element("Description")
};
if (story.Pictures == null) story.Pictures = new List<StoryPicture>();
story.Pictures.Add(newPicture);
break;
default:
Console.WriteLine("Error");
Console.ReadLine();
break;
}
break;
case State.SUBTITLE :
switch (child.Name.LocalName)
{
case "Body" :
newItem.ItemType = "SubTitle";
newItem.ItemText = (string)child;
break;
case "Subtitle":
newItem = new Item();
if (story.Items == null) story.Items = new List<Item>();
story.Items.Add(newItem);
break;
default:
Console.WriteLine("Error");
Console.ReadLine();
break;
}
break;
}
}
return story;
}
}
public class StoryPicture
{
public string ImageHref { get; set; }
public string Credit { get; set; }
public string Description { get; set; }
}
public class Item
{
public string ItemType { get; set; } // Possible: Body or Subtitle
public string ItemText { get; set; }
}
}
If you take out the <Root> tag in your XML and change your method for the deserialization to:
public InputXmlModel GetInputXmlModelByXmlFile(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(Article));
using (FileStream fileStream = new FileStream("<PathToFile>", FileMode.Open))
{
Article result = (Article)serializer.Deserialize(fileStream);
}
}
It should work.
My below my code which generate an xml and return the generated xml but the current format is not the structure expected:
My Class:
public class response
{
[StringLength(64)]
public string reference { get; set; }
public int responseCode { get; set; }
[StringLength(140)]
public string responseMessage { get; set; }
[StringLength(32)]
public string transactionId { get; set; }
public List<account> accounts { get; set; }
}
public class account
{
[StringLength(64)]
public string account_number { get; set; }
}
rs = "<?xml version='1.0' encoding='UTF-8'?><USSDResponse><Status>true</Status><StatusMessage>Account details returned for 08069262257</StatusMessage><SessionID>31853F5C-A1C1-2A6F-E054-8E1F65C33B15</SessionID><AccountNumber><AccountNo>0003893369</AccountNo><AccountStatus>ACCOUNT OPEN REGULAR</AccountStatus><AvailableBalance>17674.69</AvailableBalance><AccountName>IYEKE IKECHUKWU I.</AccountName><AccountCurrency>NGN</AccountCurrency><ProductName>CURRENT STAFF</ProductName></AccountNumber><AccountNumber><AccountNo>0064612613</AccountNo><AccountStatus>ACCOUNT OPEN REGULAR</AccountStatus><AvailableBalance>201132.18</AvailableBalance><AccountName>IKECHUKWU ISRAEL IYEKE</AccountName><AccountCurrency>NGN</AccountCurrency><ProductName>HIDA</ProductName></AccountNumber></USSDResponse>";
x.LoadXml(rs);
status = x.GetElementsByTagName("Status")[0].InnerText;
SessionID = x.GetElementsByTagName("SessionID")[0].InnerText;
if (status != null && status == "true")
{
var accts = x.GetElementsByTagName("AccountNo");
var names = x.GetElementsByTagName("ProductName");
if (accts.Count >= 2)
{
//var AcctNo = new accounts();
foreach (XmlElement a in accts)
{
var acctNo = a.InnerText.Substring(0, 10);
accounts.Add(new account { account_number = acctNo });
}
o.accounts = accounts;
o.reference = reference;
o.responseCode = 6;
o.responseMessage = "Please you can only purchase airtime in naira only and no kobo inclusive.";
o.transactionId = "Nil";
logger.Info($"Wrong amount: {amountString} including kobo entered by the user for mobile number: {msisdn}");
return o;
}
}
Result:
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<reference>565695769-8490890112091</reference>
<responseCode>6</responseCode>
<responseMessage>Please you can only purchase airtime in naira only and no kobo inclusive.</responseMessage>
<transactionId>Nil</transactionId>
<accounts>
<account><account_number>0003893369</account_number></account>
<account><account_number>0064612613</account_number></account>
</accounts>
</response>
My above code generate an xml and return the generated xml but the current format is not the structure expected:But I want the result to be exactly with the removal of tag <account_number></account_number>:
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<reference>565695769-8490890112091</reference>
<responseCode>6</responseCode>
<responseMessage>Please you can only purchase airtime in naira only and no kobo inclusive.</responseMessage>
<transactionId>Nil</transactionId>
<accounts>
<account>0003893369</account>
<account>0064612613</account>
</accounts>
</response>
Modify your class structure as below,
public class Accounts {
public Accounts ()
{
Account = new List<string>();
}
public List<string> Account { get; set; }
}
public class Response {
...
public Accounts Accounts { get; set; }
}
I am trying to create a WCF-service, which can return me an XML document looking something like this:
<alert>
<identifier>SecretID</identifier>
<info>
<valueName>Name1</valueName>
<value>Info1</value>
</info>
<info>
<valueName>Name2</valueName>
<value>Info2</value>
</info>
</alert>
The DTO of this file would look something like this:
[DataContract(Name = "alert", Namespace = "")]
public class Alert
{
[DataMember(Name = "identifier")]
public string identifier { get; set; }
[DataMember(Name = "info")]
public List<Info> Info { get; set; }
}
[DataContract(Name = "info", Namespace = "")]
public class Info
{
[DataMember(Name = "valueName")]
public string ValueName { get; set; }
[DataMember(Name = "value")]
public string Value { get; set; }
}
However, when I try with the following:
var alert = new Alert()
{
Identifier = "SecretID",
Info = new List<Info>
{
new Info() {ValueName = "Name1", Value = "Info1"},
new Info() {ValueName = "Name2", Value = "Info2"},
}
}
I get:
<alert>
<identifier>SecretID</identifier>
<info>
<info xmlns="">
<valueName>Name1</valueName>
<value>Info1</value>
</info>
<info xmlns="">
<valueName>Name2</valueName>
<value>Info2</value>
</info>
</info>
</alert>
I don't need the extra <info> tag, and the namespace xmlns="" would be nice to have removed too. What should I do to get rid of it?
You can use dictionary or key value pairs as mentioned below:
[DataContract(Name = "alert", Namespace = "")]
public class Alert
{
[DataMember(Name = "identifier")]
public string identifier { get; set; }
[DataMember(Name = "info")]
public KeyValuePair<string, string> Info { get; set; }
}
var alert = new Alert()
{
Identifier = "SecretID",
Info = new KeyValuePair<string, string>()
}
I want to serialize a class Ticket into xml. I get the error :"XmlAttribute/XmlText cannot be used to encode complex type" because of my customfield class.
This is how the xml for customfields should look like ( the attribute array is nesseray but I don't understand how to create it):
<custom_fields type="array">
<custom_field name="Standby Reason" id="6">
<value/>
</custom_field>
<custom_field name="Close Date" id="84">
Class Ticket
public class Ticket
{
[XmlElement("custom_fields")]
public CustomFields Custom_fields { get; set; }
Class CustomFields
[Serializable]
public class CustomFields
{
[XmlAttribute("array")]
public List<CustomField> custom_field { get; set; }
Class CustomField
[Serializable]
public class CustomField
{
[XmlIgnore]
public string Name { get; set; }
[XmlElement]
public int Id { get; set; }
[XmlElement]
public string Value { get; set; }
Serialize Method
public string Serialize(object obj)
{
var nsSerializer = new XmlSerializerNamespaces();
nsSerializer.Add(String.Empty, String.Empty);
var serializer = new XmlSerializer(typeof(Ticket), String.Empty);
using (StringWriter writer = new StringWriter())
{
ExtendedXmlTextWriter xmlTextWriter = new ExtendedXmlTextWriter(writer);
serializer.Serialize(xmlTextWriter, obj, nsSerializer);
//return writer.ToString();
XElement root = new XElement("custom_fields", new XAttribute("type", "array"),
new XElement("custom_field",
new XAttribute("name", "Standby Reason"),
new XAttribute("id", 6)
), new XElement("value"),
new XElement("custom_field",
new XAttribute("name", "Close Date"),
new XAttribute("id", 84)
)
);
return (writer.ToString() + root.ToString());
}
Sometimes Linq To Xml can be very helpful
XElement root = new XElement("ticket",
new XElement("custom_fields",
new XAttribute("type", "array"),
new XElement("custom_field",
new XAttribute("name", "Standby Reason"),
new XAttribute("id", 6)
),
new XElement("value"),
new XElement("custom_field",
new XAttribute("name", "Close Date"),
new XAttribute("id", 84)
)
)
);
string xml = root.ToString();
OUTPUT:
<ticket>
<custom_fields type="array">
<custom_field name="Standby Reason" id="6" />
<value />
<custom_field name="Close Date" id="84" />
</custom_fields>
</ticket>
Class Ticket
public class Ticket
{
[XmlElement("custom_fields")]
public CustomFields Custom_fields { get; set; }
Class CustomFields
[Serializable]
public class CustomFields
{
[XmlArray("array"), XmlArrayItem("custom_field")]
public List<CustomField> custom_field { get; set; }
Class CustomField
[Serializable]
public class CustomField
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("id")]
public int Id { get; set; }
[XmlElement("value")]
public string Value { get; set; }
XML:
<ticket>
<custom_fields>
<array>
<custom_field name="Standby Reason" id="6"><value /></custom_field>
<custom_field name="Close Date" id="84"><value /></custom_field>
</array>
</custom_fields>
</ticket>
List<CustomField> can not be XML attribute.