Error on deserializing a simple XML into a class object - c#

My XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAddressDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AddressDetails>
<DbServerName>2k8</DbServerName>
</AddressDetails>
<AddressDetails>
<DbServerName>2k8R2D3</DbServerName>
</AddressDetails>
</ArrayOfAddressDetails>
And I created two classes for it as follows:
public class AddressDetails
{
public string DbServerName { get; set; }
}
}
and another class to hold a list of those:
public class AddressList
{
public List<AddressDetails> addressList= new List<AddressDetails>() ;
}
And this is how I am deserializng it:
XmlSerializer deSerializer = new XmlSerializer(typeof(AddressList));
TextReader reader = new StreamReader(#"C:\TEMP\MyXML.xml");
Object obj = deSerializer.Deserialize(reader);
AddressList adrsList = (AddressList)obj;
reader.Close();
But on Deserialize method I get this error:

You have to decorate:
AddressList class with XmlRoot attribute like this [XmlRoot("ArrayOfAddressDetails")]
addressList field with XmlElement attribute like this [XmlElement("AddressDetails")]
You have this at the end:
[XmlRoot("ArrayOfAddressDetails")]
public class AddressList
{
[XmlElement("AddressDetails")]
public List<AddressDetails> addressList = new List<AddressDetails>();
}

Related

C# serialize object to XML with element containing XML text without escaping

I searched and tried some attributes but none of them worked for my below scenario:
public class ObjSer
{
[XmlElement("Name")]
public string Name
{
get; set;
}
}
//Code to serialize
var obj = new ObjSer();
obj.Name = "<tag1>Value</tag1>";
var stringwriter = new System.IO.StringWriter();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
serializer.Serialize(stringwriter, obj);
Output would be as follows:
<ObjSer><Name><tag1>Value</tag1></Name></ObjSer>
But I need output as:
<ObjSer><Name><tag1>Value</tag1></Name></ObjSer>
Scenario 2: In some cases I need to set:
obj.Name = "Value";
Is there any attribute or method I can override to make it possible?
You can't with default serializer. XmlSerializer does encoding of all values during serialization.
If you want your member to hold xml value, it must be XmlElement. Here is how you can accomplish it
public class ObjSer
{
[XmlElement("Name")]
public XmlElement Name
{
get; set;
}
}
var obj = new ObjSer();
// <-- load xml
var doc = new XmlDocument();
doc.LoadXml("<tag1>Value</tag1>");
obj.Name = doc.DocumentElement;
// --> assign the element
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
serializer.Serialize(Console.Out, obj);
Output:
<?xml version="1.0" encoding="IBM437"?>
<ObjSer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>
<tag1>Value</tag1>
</Name>
</ObjSer>
UPDATE:
In case if you want to use XElement instead of XmlElement, here is sample on how to do it
public class ObjSer
{
[XmlElement("Name")]
public XElement Name
{
get; set;
}
}
static void Main(string[] args)
{
//Code to serialize
var obj = new ObjSer();
obj.Name = XDocument.Parse("<tag1>Value</tag1>").Document.FirstNode as XElement;
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
serializer.Serialize(Console.Out, obj);
}
No, you can't. It is the natural and usual behaviour of the xml serializer. The serializer doesn't have to handle within the XML strings. So, it escapes the xml as expected.
You should decode the escaped string again while deserializing it.
If you want to add dynamically elements in the XML I suggest you to use Linq to XML and you can create tag1 or another kind of elements easily.
I would suggest serializing to an XDocument then converting that to a string and manually unescaping the desired part and writing it to a file. I would say this would give you the least headache it shouldn't be more than a couple lines of code. If you need it I can provide some code example if needed.
I found one more way of changing the type
public class NameNode
{
public string tag1
{
get; set;
}
[XmlText]
public string Value
{
get; set;
}
}
public class ObjSer
{
[XmlElement("Name")]
public NameNode Name
{
get; set;
}
}
To set value of Name:
var obj = new ObjSer();
var valueToSet = "<tag1>Value</tag1>";
//or var valueToSet = "Value";
//With XML tag:
if (valueToSet.Contains("</"))
{
var doc = new XmlDocument();
doc.LoadXml(valueToSet);
obj.Name.tag1 = doc.InnerText;
}
else //Without XML Tags
{
obj.Name.Value = senderRecipient.Name;
}
This solution will work in both cases, but has limitation. It will work only for predefined tags (ex. tag1)

How set xmlns prefix in deserialization class for the root element

I used xmltocsharp site to create the class helper to deserialize an specific XML, but is not working, and the problem is in the root element. This is the root element (RESP_HDR and RESP_BODY were collapsed):
<?xml version="1.0" encoding="UTF-8"?>
<SII:RESPUESTA xmlns:SII="http://www.sii.cl/XMLSchema">
+ <SII:RESP_HDR>
+ <SII:RESP_BODY>
</SII:RESPUESTA>
And this is the root element class generated by xmltocsharp site:
[XmlRoot(ElementName = "RESPUESTA", Namespace = "http://www.sii.cl/XMLSchema")]
public class RESPUESTA
{
[XmlElement(ElementName = "RESP_HDR", Namespace = "http://www.sii.cl/XMLSchema")]
public RESP_HDR RESP_HDR { get; set; }
[XmlElement(ElementName = "RESP_BODY", Namespace = "http://www.sii.cl/XMLSchema")]
public RESP_BODY RESP_BODY { get; set; }
[XmlAttribute(AttributeName = "SII", Namespace = "http://www.w3.org/2000/xmlns/")]
public string SII { get; set; }
}
The issue is that the class fails to deserialize a XML like the showed before, but success with this:
<?xml version="1.0" encoding="UTF-8"?>
<RESPUESTA xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SII="http://www.sii.cl/XMLSchema" xmlns="http://www.sii.cl/XMLSchema">
+ <SII:RESP_HDR>
+ <SII:RESP_BODY>
</RESPUESTA>
The difference is in the namespaces, even if a create the object and serialize it, this will be the result. So, what should be changed in the class to make it work with the original XML?
UPDATE:
Looking closer I found the really issue, is in the root element still, but I notice the missing xmlns prefix in the root tag, how can I set it in the helper class?
EDIT:
This is an XML sample from the service response:
<?xml version="1.0" encoding="UTF-8"?>
<SII:RESPUESTA xmlns:SII="http://www.sii.cl/XMLSchema">
<SII:RESP_HDR>
<SII:ESTADO>0</SII:ESTADO>
<SII:GLOSA/>
</SII:RESP_HDR>
<SII:RESP_BODY>
<DATOS_CONSULTA>
<RUT>80182144-3</RUT>
<TIPO_CONSULTA>DEUDOR</TIPO_CONSULTA>
<DESDE_DDMMAAAA>01042017</DESDE_DDMMAAAA>
<HASTA_DDMMAAAA>01052017</HASTA_DDMMAAAA>
</DATOS_CONSULTA>
<CESION>
<VENDEDOR>11455447-9</VENDEDOR>
<ESTADO_CESION>Cesion Vigente</ESTADO_CESION>
<DEUDOR>80182144-3</DEUDOR>
<MAIL_DEUDOR/>
<TIPO_DOC>33</TIPO_DOC>
<NOMBRE_DOC>Factura Electronica</NOMBRE_DOC>
<FOLIO_DOC>107</FOLIO_DOC>
<FCH_EMIS_DTE>2017-04-04</FCH_EMIS_DTE>
<MNT_TOTAL>3324860</MNT_TOTAL>
<CEDENTE>11455447-9</CEDENTE>
<RZ_CEDENTE>JHON DOE</RZ_CEDENTE>
<MAIL_CEDENTE>jjdoe#gmail.com</MAIL_CEDENTE>
<CESIONARIO>762327129-7</CESIONARIO>
<RZ_CESIONARIO>capital sa</RZ_CESIONARIO>
<MAIL_CESIONARIO>xcap#capital.com</MAIL_CESIONARIO>
<FCH_CESION>2017-04-05 13:15</FCH_CESION>
<MNT_CESION>3324860</MNT_CESION>
<FCH_VENCIMIENTO>2017-06-04</FCH_VENCIMIENTO>
</CESION>
<CESION>
<VENDEDOR>11455447-9</VENDEDOR>
<ESTADO_CESION>Cesion Vigente</ESTADO_CESION>
<DEUDOR>80182144-3</DEUDOR>
<MAIL_DEUDOR/>
<TIPO_DOC>33</TIPO_DOC>
<NOMBRE_DOC>Factura Electronica</NOMBRE_DOC>
<FOLIO_DOC>34</FOLIO_DOC>
<FCH_EMIS_DTE>2017-03-01</FCH_EMIS_DTE>
<MNT_TOTAL>1725500</MNT_TOTAL>
<CEDENTE>11455447-9</CEDENTE>
<RZ_CEDENTE>JOE DOE</RZ_CEDENTE>
<MAIL_CEDENTE>jd#gmail.com</MAIL_CEDENTE>
<CESIONARIO>762327129-7</CESIONARIO>
<RZ_CESIONARIO>Capital S.A.</RZ_CESIONARIO>
<MAIL_CESIONARIO>jcap#capital.com</MAIL_CESIONARIO>
<FCH_CESION>2017-04-05 17:27</FCH_CESION>
<MNT_CESION>1725500</MNT_CESION>
<FCH_VENCIMIENTO>2017-03-01</FCH_VENCIMIENTO>
</CESION>
</SII:RESP_BODY>
</SII:RESPUESTA>
So far the only way that I can make it work is with a really ugly solution, this should not be considered as an answer to the problem!!.
//Query service to obtain XML response
string xmlResponse = siiClient.QueryDocuments(documentsRequest);
//Replace RESPUESTA tags in the XML response, remove the prefix SII
var replacedXML = xmlResponse .Replace("SII:RESPUESTA", "RESPUESTA" );
//Load XML string into XmlDocument
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(replacedXML);
//Add missing namespaces
xDoc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
xDoc.DocumentElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
xDoc.DocumentElement.SetAttribute("xmlns", "http://www.sii.cl/XMLSchema");
//Now deserialization will work
var documentResponse = xDoc.ParseXML<RESPUESTA>();
The ideal solution will take the XML Response and deserialize it directly without any preprocessing like:
//Query service to obtain XML response
string xmlResponse = siiClient.QueryDocuments(documentsRequest);
//ParseXML is an extension method, it can handle an string or an XmlDocument
var documentResponse = xmlResponse.ParseXML<RESPUESTA>();
Note: ParseXML is based on #Damian's answer
I took your xml:
<?xml version="1.0" encoding="UTF-8"?>
<SII:RESPUESTA xmlns:SII="http://www.sii.cl/XMLSchema">
<SII:RESP_HDR/>
<SII:RESP_BODY/>
</SII:RESPUESTA>
I took you class:
[XmlRoot(ElementName = "RESPUESTA", Namespace = "http://www.sii.cl/XMLSchema")]
public class RESPUESTA
{
[XmlElement(ElementName = "RESP_HDR", Namespace = "http://www.sii.cl/XMLSchema")]
public RESP_HDR RESP_HDR { get; set; }
[XmlElement(ElementName = "RESP_BODY", Namespace = "http://www.sii.cl/XMLSchema")]
public RESP_BODY RESP_BODY { get; set; }
[XmlAttribute(AttributeName = "SII", Namespace = "http://www.w3.org/2000/xmlns/")]
public string SII { get; set; }
}
public class RESP_HDR { }
public class RESP_BODY { }
Just added two empty class stub.
Try System.Xml.Serialization.XmlSerializer:
RESPUESTA respuesta;
var xs = new XmlSerializer(typeof(RESPUESTA));
using (var fs = new FileStream("test.xml", FileMode.Open))
respuesta = (RESPUESTA)xs.Deserialize(fs);
It works! I don't understand how and why it does not work for you.

deserialization XML in C#

can somebody help with this problem. I have created class for deserialization data from XML. But when I programme compile VS shows me this exception InvalidOperationException: There is an error in XML document(2,2) so I guess that I defined attribute Month in class Store. I tried to type into int and without successfull please help me... Here is the code of problem:
<?xml version="1.0" encoding="utf-8" ?>
<Store>
<StoreS Month="2">
<Amount>159</Amount>
<Mod_date> 20.3.2014 18:19:18</Mod_date>
</StoreS>
<StoreS Month="2">
<Amount>270</Amount>
<Mod_date> 20.3.2014 18:19:40</Mod_date>
</StoreS>
</Store>
The class into which I wanna to deserialize data is written this way:
[XmlRoot("Store"),XmlType("Store")]
public class Store
{
[XmlElement("StoreS")]
public List<RecordStore> StoreS = new List<RecordStore>();
[XmlAttribute("Month")]
public string Month { get; set; }
}
public class RecordStore
{
[XmlElement("Amount")]
public int amount{get;set;}
[XmlElement("Mod_date")]
public DateTime mod_date { get; set; }
}
Thank you very much for your help.
Your xml has bad datetime format, it should be like 2014-03-21T00:00:00, for example
as you serialize in the same way you can de-serialize the object
public void Serialize<T>(T details)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (TextWriter writer = new StreamWriter("Xml.xml"))
{
serializer.Serialize(writer, details);
}
}
public void Deserialize<T>(out T obj)
{
XmlSerializer serializer = new XmlSerializer(typeof (T));
using (TextReader reader = new StreamReader("Xml.xml"))
{
obj = (T)serializer.Deserialize(reader);
}
}

Deserialize a class with interface

I have a class which contain an interface member variable.How can i deserialize this class
interface ISensor { }
[Serializable]
class Sensor: ISensor { }
[Serializable]
class Root
{
[XmlElement("Sensor")]
public List<ISensor> SensorList{ get; set; }
}
My XML will be like this
<?xml version="1.0" encoding="us-ascii"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Sensor >
<SensorName>Name1</SensorName>
<SensorValue>0.0</SensorValue>
</Sensor>
<Sensor>
<SensorName>Name2</SensorName>
<SensorValue>148.00</SensorValue>
</Sensor>
</Root>
Assuming you are using XmlSerializer, there are two changes required for both serialization and deserialization:
Tell the serializer the list of types that can appear, in other words the types that inherit from Sensor.
Use a class for the List rather than an interface, in other words replace List<ISensor> with List<Sensor>.
Unfortunately, the XmlSerializer does not handle interfaces in the way you want. See XmlSerializer serialize generic List of interface for more information.
If using a base class is not an option You could write your own XML serializer by implementing IXmlSerializable. Override ReadXml and parse the XML manually.
For example:
public interface ISensor { }
[Serializable]
public class Sensor : ISensor { }
[Serializable]
public class Root
{
// Changed List<ISensor> to List<Sensor>. I also changed
// XmlElement to XmlArray so it would appear around the list.
[XmlArray("Sensor")]
public List<Sensor> SensorList { get; set; }
}
[Serializable]
public class SensorA : Sensor
{
[XmlElement("A")]
public string A { get; set; }
}
[Serializable]
public class SensorB : Sensor
{
[XmlElement("B")]
public string B { get; set; }
}
class Program
{
public static void Main(string[] args)
{
XmlSerializer xmlSerializer;
Root root = new Root();
root.SensorList = new List<Sensor>();
root.SensorList.Add(new SensorA() {A = "foo"});
root.SensorList.Add(new SensorB() {B = "bar"});
// Tell the serializer about derived types
xmlSerializer = new XmlSerializer(typeof (Root),
new Type[]{typeof (SensorA), typeof(SensorB)});
StringBuilder stringBuilder = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(stringBuilder))
{
xmlSerializer.Serialize(stringWriter, root);
}
// Output the serialized XML
Console.WriteLine(stringBuilder.ToString());
Root root2;
using (StringReader stringReader = new StringReader(stringBuilder.ToString()))
{
root2 = (Root) xmlSerializer.Deserialize(stringReader);
}
}
}
The output from the Console.WriteLine statement is:
<?xml version="1.0" encoding="utf-16"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Sensor>
<Sensor xsi:type="SensorA">
<A>foo</A>
</Sensor>
<Sensor xsi:type="SensorB">
<B>bar</B>
</Sensor>
</Sensor>
</Root>

Custom XML-element name for base class field in serialization

How can I change XML-element name for field inherited from base class while doing serialization?
For example I have next base class:
public class One
{
public int OneField;
}
Serialization code:
static void Main()
{
One test = new One { OneField = 1 };
var serializer = new XmlSerializer(typeof (One));
TextWriter writer = new StreamWriter("Output.xml");
serializer.Serialize(writer, test);
writer.Close();
}
I get what I need:
<?xml version="1.0" encoding="utf-8"?>
<One xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OneField>1</OneField>
</One>
Now I have created new class inherited from A with additional field and custom XML element name for it:
public class Two : One
{
[XmlElement("SecondField")]
public int TwoField;
}
Serialization code:
static void Main()
{
Two test = new Two { OneField = 1, TwoField = 2 };
var serializer = new XmlSerializer(typeof (Two));
TextWriter writer = new StreamWriter("Output.xml");
serializer.Serialize(writer, test);
writer.Close();
}
As a result I get next output:
<?xml version="1.0" encoding="utf-8"?>
<Two xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OneField>1</OneField>
<SecondField>2</SecondField>
</Two>
The problem is that I want to change OneField in this output to FirstField without touching base class code (because I will use it too and the names must be original). How can I accomplish this?
Thank you.
Try this:
public class Two : One
{
private static XmlAttributeOverrides xmlOverrides;
public static XmlAttributeOverrides XmlOverrides
{
get
{
if (xmlOverrides == null)
{
xmlOverrides = new XmlAttributeOverrides();
var attr = new XmlAttributes();
attr.XmlElements.Add(new XmlElementAttribute("FirstField"));
xmlOverrides.Add(typeof(One), "OneField", attr);
}
return xmlOverrides;
}
}
[XmlElement("SecondField")]
public string TwoField;
}
And your serializer init is a lot easier:
var xmls = new System.Xml.Serialization.XmlSerializer(typeof(Two), Two.XmlOverrides);
Here's a workaround: Override the fields in the subclass and mark the overriden field with whatever name you need. For example,
class One
{
public int OneField { get; set; }
}
class Two : One
{
[XmlElement("FirstField")]
public new int OneField
{
get { return base.OneField; }
set { base.OneField = value; }
}
}

Categories