Object element from XML De-serialization not filling Attributes are - c#

I am receiving back from a Web Service XML that looks like this:
<RESULT>
<GRP ID="INP">
<FLD NAME="SORDNO" TYPE="Char"></FLD>
<FLD NAME="SITENO" TYPE="Char">999</FLD>
</GRP>
<TAB DIM="100" ID="POS" SIZE="0"/>
<TAB DIM="500" ID="ERR" SIZE="2">
<LIN NUM="1">
<FLD NAME="ERRORS" TYPE="Char"/>
</LIN>
<LIN NUM="2">
<FLD NAME="ERRORS" TYPE="Char">Site code 999 is not valid</FLD>
</LIN>
</TAB>
I want to codify the XML into an object so I created a class that seems to match the XML structure:
namespace DropShipmentFulfillment.SagePOCreateProcess
{
public class SagePOCreateResponse1
{
[XmlRoot("RESULT")]
public class RESULT
{
[XmlElement("GRP")]
public RESULTGRP GRP;
[XmlElement("TAB")]
public RESULTTAB[] TAB;
}
public partial class RESULTGRP
{
[XmlAttribute("ID")]
public string ID;
[XmlElement("FLD")]
public RESULTFLD[] FLD;
}
public partial class RESULTTAB
{
[XmlAttribute("DIM")]
public string DIM;
[XmlAttribute("ID")]
public string ID;
[XmlAttribute("SIZE")]
public string SIZE;
[XmlElement("LIN")]
public RESULTLIN[] LIN;
}
public partial class RESULTLIN
{
[XmlAttribute("NUM")]
public string NUM;
[XmlElement("FLD")]
public RESULTFLD FLD;
}
public partial class RESULTFLD
{
[XmlAttribute("NAME")]
public string NAME;
[XmlAttribute("TYPE")]
public string TYPE;
}
}
}
When I perform the serialization I execute this code:
private SagePOCreateResponse1.RESULT GetPOResponse(SageWeb.CAdxResultXml sageSvcResponse)
{
string xml = sageSvcResponse.resultXml;
XmlSerializer serializer = new XmlSerializer(typeof(SagePOCreateResponse1.RESULT));
StringReader rdr = new StringReader(xml);
SagePOCreateResponse1.RESULT resultingMessage = (SagePOCreateResponse1.RESULT)serializer.Deserialize(rdr);
return resultingMessage;
}
When I execute the program I do not get an error indicating that the class structure does not match the XML structure, but specifically, the FLD element does not contain a value. For example, if I stop to take a look at a specific field (FLD), in GRP I get the attributes values but not the value of the element:
?objectResponse.GRP.FLD[0]
{DropShipmentFulfillment.SagePOCreateProcess.SagePOCreateResponse1.RESULTFLD}
NAME: "SORDNO"
TYPE: "Char"
It seems like the serializer is not putting the element value in place though I think I have named it right and I get no errors.
What am I missing? Why would it resolve the attributes and not the element values?
I did try doing the XSD creation to C# Class route, but when I had to deal with dynamic lines (LIN) I tried creating my own class.

Thanks to jdweng I was able to find a solution. Keeping all things the same from my question I changed one class:
public partial class RESULTFLD
{
[XmlAttribute("NAME")]
public string NAME;
[XmlAttribute("TYPE")]
public string TYPE;
[System.Xml.Serialization.XmlTextAttribute()]
public string Value;
}
I needed this line "[System.Xml.Serialization.XmlTextAttribute()]" instead of just the "[XmlText]" line.
The class now deserilizes the XML so I get both attribute values and innerText values.

Related

XML deserialization of derived classes C#

I have next code:
[XmlRoot(ElementName = "Container")]
public class Container {
[XmlArray("Items", IsNullable = false)]
[XmlArrayItem("Item")]
public List<BaseItem> Items { get; set; } = new List<BaseItem>();
}
public class BaseItem {
[XmlAttribute("SomeField")]
public string SomeField {get;set;}
}
public class DerivedItem : BaseItem {
[XmlAttribute("OtherField")]
public string OtherField {get;set;}
}
How can I deserialize:
<Container>
<Items>
<Item SomeField="Value"/>
<Item SomeField="Value" OtherField="OtherValue"/>
</Items>
</Container>
so, Items field in Container object can contain BaseItem and DerivedItem objects from XML above?
Well, you can't because when deserializing the XmlSerializer does not determine when to use BaseItem or DerivedItem. So imho you should not use inheritance here.
Now what you possibly needed is to know if OtherField is specified or not.
Fortunately this is something the XmlSerializer CAN do. For this you need to add to your class representing the item a bool property OtherFieldSpecified which indicates if OtherField is well... specified.
You should use
// Define other methods and classes here
[XmlRoot(ElementName = "Container")]
public class Container {
[XmlArray("Items", IsNullable = false)]
[XmlArrayItem("Item")]
public List<DerivedItem> Items { get; set; }
}
public class DerivedItem
{
[XmlAttribute("SomeField")]
public string SomeField {get;set;}
[XmlAttribute("OtherField")]
public string OtherField {get;set;}
public bool OtherFieldSpecified {get;set;}
}
So a little adjust working linqPad scribble gave me this code:
void Main()
{
var xml = #"
<Container>
<Items>
<Item SomeField=""Value""/>
<Item SomeField=""Value"" OtherField=""OtherValue""/>
</Items>
</Container>
";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
var ser = new XmlSerializer(typeof(Container));
var container = (Container) ser.Deserialize(stream);
container.Dump();
}
Can we start with you not posting valid XML?
Elements are unique. Inheritance (derived types) MUST change the element name. THis is XML standard because the element name is how the XML Schema determines what elements exist.
YOu do that then you can add multiple XmlArrayItem entries - there is a second overload that takes the type of the included item.

Deserialize XML element from InnerText and Value attribute

I have XML Serializable class with property Name
[Serializable]
public class Item
{
[XmlElement("name")]
public string Name { get; set; }
}
and I want it to be able to deserialize XML file that I have in two ways:
<item>
<name>Name</name>
</item>
and
<item>
<name value="Name" />
</item>
The first works fine but what should I do to be able to deserialize the second also with the same class?
XML Serialization attributes work both with serialization and deserialization. If we'll assume that it might be possible to use attributes for deserializing instance of Item from two different xml structures, then how serialization should work - should it serialize instance name to element value, or to attribute? Or to both? That's why you cannot deserialize two different xml structures into single class. Use two different classes or deserialize it manually without usage of XML Serialization attributes.
I found another way to solve my problem using only one class maybe someone will find this useful
[Serializable]
public class Item
{
[XmlElement("name")]
public NameElement NameElement { get; set; }
}
public class NameElement
{
[XmlAttribute("value")]
public string Value { get; set; }
[XmlText]
public string Text { get; set; }
[XmlIgnore]
public string Name
{
get { return String.IsNullOrEmpty(this.Value) ? this.Text : this.Value; }
set { this.Value = value; }
}
}
Maybe it's not super elegant but it works in both cases and uses the same class.
Since you have mentioned that XML data is coming from external sources, so obviously you don't have control over that.
Therefore you can follow any of the option as below:
Create separate class per XML data structure, because as far I know there is no way to control XML Deserialization when using XmlSerializer
You can use XDocument to read the XML by self, to overcome this limitation.
If going by second idea, I have created small Console Application to demonstrate that.
Main piece of code is as below:
MemoryStream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlData));
XDocument doc = XDocument.Load(xmlStream);
var records = from record in doc.Descendants("item").Descendants()
select new Item(!record.IsEmpty ? record.Value : record.Attribute("value").Value);
Here I'm reading the element using LinqToXml and checking if the element is not empty, i.e. Value is not blank, then use Value otherwise read the value from element's Attribute.
Console application (Complete code):
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace Console.TestApp
{
class Program
{
static string xmltypeFirst = #"<item>
<name>John</name>
</item>";
static string xmltypeSecond = #"<item>
<name value='Smith' />
</item>";
static void Main(string[] args)
{
var data = xmltypeFirst;
var result = Deserialize(data).ToList();
Console.WriteLine("Name: " + result[0].Name);
data = xmltypeSecond;
result = Deserialize(data).ToList();
Console.WriteLine("Name: " + result[0].Name);
Console.WriteLine("Press any to key to exit..");
Console.ReadLine();
}
private static IEnumerable<Item> Deserialize(string xmlData)
{
MemoryStream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlData));
XDocument doc = XDocument.Load(xmlStream);
var records = from record in doc.Descendants("item").Descendants()
select new Item(!record.IsEmpty ? record.Value : record.Attribute("value").Value);
return records;
}
}
[Serializable]
public class Item
{
public Item(string name)
{
this.Name = name;
}
[XmlElement("name")]
public string Name { get; set; }
}
}
Note: To run this you will need to add reference to System.Xml.Linq.dll in your project.
Reference: here

Serialize Class to XML issue with XmlRoot Declaration

Im serializing my class to XML. I have an issue with the root element of one of my classes is NOT being named properly.
The complete XML structure should look like the following.
<Workflow>
<Name>My Workflow</Name>
<Description />
<Modules>
<Module Name="Intro" MenuText="IntroText" />
</Modules>
</Workflow>
However Im getting this result
<Workflow>
<Name>My Workflow</Name>
<Description />
<Modules>
<WorkflowModule Name="Intro" MenuText="IntroText" />
</Modules>
</Workflow>
I want the element "WorkflowModule" to be called "Module" however the problem is that I already have another class called Module. So to get around this problem I called it a WorkflowModule and put a class XmlRoot() declartion like so;
[XmlRoot("Module")]
public class WorkflowModule
{...}
But when I serialize the Workflow class it still comes up with WorkflowModule.
Here are my 2 classes classes;
[XmlRoot("Workflow")]
public class Workflow
{
private string _name;
private string _description;
private List<WorkflowModule> _modules = new List<WorkflowModule>();
[XmlElement("Name")]
public String Name
{
get { }
set { }
}
[XmlElement("Description")]
public String Description
{
get { }
set { }
}
[XmlArrayItem(typeof(WorkflowModule))]
public List<WorkflowModule> Modules
{
get { }
set { }
}
}
[XmlRoot("Module")]
public class WorkflowModule
{
private string _name;
private string _menu_text;
public WorkflowModule()
{
}
[XmlAttribute("Name")]
public String Name
{
get { }
set { }
}
[XmlAttribute("MenuText")]
public String MenuText
{
get { }
set { }
}
}
}
Set the element name within XmlArrayItem attrubute:
[XmlArrayItem(typeof(WorkflowModule), ElementName = "Module")]
There are many ways to control it as defined in this duplicate post How do I Set XmlArrayItem Element name for a List<Custom> implementation?
These attribute control serialize from the this object's perspective as it transverses nested objects
[XmlArray("RootArrayElementNameGoesHere")]
[XmlArrayItem(typeof(Workflow), ElementName="ArrayItemElementNameGoesHere")]
public List<WorkflowModule> Modules
This attribute redefining the element name but can be overwritten with the local [XmlArrayItem] or [XmlElement] attributes to provides local override from the owning objects serialization
[XmlType(TypeName = "UseThisElementNameInsteadOfClassName")]
public class WorkflowModule
This attribute is only honored when its the direct object being serialized
[XmlRoot("UseThisElementNameWhenItIsTheRoot")]
public class WorkflowModule

Set XML serialziation resulting doc root

I've got another problem (which might not be an issue in terms of coding problems) but more of principle..been bugging me for a while. I have this c# class, as follows:
namespace SMCProcessMonitor
{
public class Config
{
[XmlElement("Recipient")]
public string recipient;
[XmlElement("Server-port")]
public int serverport;
[XmlElement("Username")]
public string username;
[XmlElement("Password")]
public string password;
[XmlElement("Program")]
public List<Programs> mPrograms = new List<Programs>();
[Serializable]
[XmlRoot("Email-Config")]
public class Email
{
public string Recipient
{
get
{
return SMCProcessMonitor.ConfigManager.mConfigurations.recipient;
}
set
{
SMCProcessMonitor.ConfigManager.mConfigurations.recipient = value;
}
}
public int ServerPort
{
get
{
return SMCProcessMonitor.ConfigManager.mConfigurations.serverport;
}
set
{
SMCProcessMonitor.ConfigManager.mConfigurations.serverport = value;
}
}
public string Username
{
get
{
return SMCProcessMonitor.ConfigManager.mConfigurations.username;
}
set
{
SMCProcessMonitor.ConfigManager.mConfigurations.username = value;
}
}
public string Password { get; set; }
}
}
I can serialize this almost fine. (i recently changed simple get; set; to the full-works as seen above, but when serialising i get something like this;
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Recipient>sd</Recipient>
<Server-port>1234</Server-port>
<Username>dk</Username>
<Password>kdkdk</Password>
</Config>
Basically I want to wrap these 4 tags in an "email-settings" tag.
Add the Serializable() and XmlRoot attributes up to the base class:
[Serializable()]
[XmlRoot("Email-Settings")]
public class Config
There are attributes to control aspects of xml serialization like this, see Controlling XML Serialization Using Attributes.
I think the one you want specifically is XmlRootAttribute.
You'll need to create an EmailSettings class that contains those 4 properties, and then make an instance of the EmailSettings class a member of your Config class.

One field xml cannot be deserialized using XmlSerializer class

I have the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<connection_state>conn_state</connection_state>
Following the msdn, I must describe it as a type for correct deserialization using XmlSerializer. So the class name points the first tag, and its fields subtags.
For example:
public class connection_state
{
public string state;
}
Will be transformed into the following xml:
<?xml version="1.0" encoding="UTF-8"?>
<connection_state>
<state>conn_state</state>
</connection_state>
But the xml I receive has only one tag. And we cannot create a field with the name of its class like:
public class connection_state
{
public string connection_state;
}
Or can?
Is there any solution for this issue?
Proper Xml has a root element with no content except other elements. If you are stuck with that tiny one-tag psuedo-XML, is there a reason you need to use XmlSerializer? Why not just create a class with a constructor that takes the literal "Xml" string:
using System.Xml.Linq;
public class connection_state {
public string state { get; set; }
public connection_state(string xml) {
this.state = XDocument.Parse(xml).Element("connection_state").Value;
}
}
Edit:
In response to OP's comment: You don't have to us an XmlSerializer; you can just read the ResponseStream directly and pass that to your connection_state constructor:
String xmlString = (new StreamReader(webResponse.GetResponseStream())).ReadToEnd();
connection_state c= new connection_state(xmlString);
Replace
public class connection_state
{
public string state;
}
to
public class connection_state
{
public string state {set; get;}
}

Categories

Resources