This is the class I'm trying to serialize
[Serializable]
public class PendingAccountInfo
{
public AccountId AccountId { get; set; }
public string EmailAddress { get; set; }
}
[Serializable]
public struct AccountId : IEquatable<AccountId>
{
private readonly int _id;
public AccountId(int id) {
_id = id;
}
public int Id {
get { return _id; }
}
...
}
This is how I do the serialization
XmlSerializer xmlserializer = new XmlSerializer(typeof(List<T>));
StringWriter stringWriter = new StringWriter();
XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };
XmlWriter writer = XmlWriter.Create(stringWriter, settings);
xmlserializer.Serialize(writer, value);
string result = stringWriter.ToString();
This is what I get
<PendingAccountInfo>
<AccountId />
<EmailAddress>test#test.com</EmailAddress>
</PendingAccountInfo>
From what I read, this should work, but I must be missing something
The problem here comes from your readonly property. As explained in this other thread, XmlSerializer only serialize property with get/set accessibility.
What you can do is either make your property settable or change your serializer.
Related
I am serializing a class that is incorrectly creating the node of one of its properties. my class structure is as follows:
Here is the top class i am serializing
[DataContract]
public class XmlReportConfiguration
{
[DataMember]
[XmlArrayItem(nameof(SingleValueDescription), typeof(SingleValueDescription))]
[XmlArrayItem(nameof(MultiValueDescription), typeof(MultiValueDescription))]
public List<Description> Descriptions { get; set; }
}
MultiValueDescription inherits from SingleValueDescription which inherits from Description.
Description has the XMlInclude tag for both the single and multi value description
My issue is when i go to serialize a Description that is of type MultiValueDescription , the xml node is serializing it as SingleValueDescription.
If i remove the XmlArrayItem Entry for the SingleValueDescription from the XmlReportConfiguration class, it then works as I want it to, but I cant remove that declaration for obvious reasons.
Is there some tag/declaration I'm missing here that is causing the serializer to ignore the child class for the node and use the parent class?
Here is the method when creating the serializer:
public static string SerializeReportConfiguration(XmlReportConfiguration config)
{
XmlSerializer serializer = new XmlSerializer(typeof(XmlReportConfiguration));
StringBuilder sb = new StringBuilder();
using (TextWriter writer = new StringWriter(sb))
{
serializer.Serialize(writer, config);
}
return sb.ToString();
}
public static XmlReportConfiguration DeserializeReportConfiguration(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(XmlReportConfiguration));
using (StringReader reader = new StringReader(xml))
{
XmlReportConfiguration sessionConfig = serializer.Deserialize(reader) as XmlReportConfiguration;
return sessionConfig;
}
}
Found a solution. The polymorphism causes issues when serializing. In my report config I used a list of the following new class and it solved my issues.
[DataContract]
public class SerializableDescription : IXmlSerializable
{
#region Properties
public SerializableDescription()
{
}
public SerializableDescription(Description description)
{
Description = description;
}
[DataMember]
public Description Description { get; set; }
#endregion
#region Methods
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
reader.MoveToContent();
string typeStr = reader.GetAttribute("Type");
Type type = TypeCache.GetTypeEx(typeStr);
XmlSerializer ser = new XmlSerializer(type);
reader.ReadStartElement();
Description = (Description)ser.Deserialize(reader);
reader.ReadEndElement();
}
public void WriteXml(XmlWriter writer)
{
Type type = Description.GetType();
writer.WriteAttributeString("Type", type.FullName);
XmlSerializer ser = new XmlSerializer(type);
ser.Serialize(writer, Description);
}
#endregion
}
I need to create xml with certain looks using serializer:
EPIT11V21 curpit11 = new EPIT11V21(curdec.id);
XmlSerializer serializer = new XmlSerializer(typeof(EPIT11V21));
using (TextWriter writer = new StreamWriter(#"F:\xml\Xml.xml"))
{
serializer.Serialize(writer, curpit11);
}
where my EPIT11V21 class is declared:
[XmlRoot("Deklaracja")]
public class EPIT11V21
{
public EPIT11V21() { }
public EPIT11V21(int XPDeclarationID)
{
this.Pouczenie = "Za uchybienie obowiązkom płatnika grozi odpowiedzialność przewidziana w Kodeksie karnym skarbowym.";
//this.Zalaczniki = "";
}
}
public Podmiot1PIT11V21 Podmiot1 = new Podmiot1PIT11V21();
public String Pouczenie { get; set; }
public String Zalaczniki{ get; set; }
with subclasses:
public class Podmiot1PIT11V21
{
public Podmiot1PIT11V21(){}
[XmlAttribute("rola")]
public string rola = "Płatnik";
public OsobaNiefizycznaPIT11V21 OsobaNieFizyczna = new OsobaNiefizycznaPIT11V21();
}
public class OsobaNiefizycznaPIT11V21
{
public OsobaNiefizycznaPIT11V21(){}
public string NIP = "0000000000";
public string PelnaNazwa = "XXXXXXXX";
}
How to decorate its parts to get such an effect:
<?xml version="1.0" encoding="UTF-8"?>
<Deklaracja xsi:schemaLocation="http://crd.gov.pl/wzor/2014/12/08/1887/ http://crd.gov.pl/wzor/2014/12/08/1887/schemat.xsd" xmlns="http://crd.gov.pl/wzor/2014/12/08/1887/" xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:zzu="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/10/07/eD/ORDZU/">
<Podmiot1 rola="Płatnik">
<etd:OsobaNiefizyczna>
<etd:NIP>0000000000</etd:NIP>
<etd:PelnaNazwa>XXXXXXXXXXXXX</etd:PelnaNazwa>
</etd:OsobaNiefizyczna>
</Podmiot1>
<Zalaczniki>
</Zalaczniki>
</Deklaracja>
i just get in second line:
<Deklaracja xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
also how to decorate that clasess so to get etd: prefix as in example ??
Your question has many different components:
In order to make the property OsobaNiefizyczna go in the correct namespace, decorate it with XmlElement and set the Namespace property correctly.
In order to add additional namespaces to the root document object, attach the [XmlNamespaceDeclarations] attribute to a public XmlSerializerNamespaces xmlsn synthetic property returning the namespaces. Also set the XmlRootAttribute.Namespace properly on the root object.
To make the root object have a schemaLocation attribute, you must add a synthetic property along the lines of the answer in How to add xsi schemalocation to root c # object XmlSerializer -- but use a property instead of a field. If you use a field you will actually add to the footprint of your class in memory.
To omit the standard xsd namespace (though I recommend you do not), use the XmlSerializer.Serialize(XmlWriter, Object, XmlSerializerNamespaces) method and pass in only the namespaces you want to see.
To make an empty Zalaczniki element show up, you need to set the property value to string.Empty.
Putting these together we get:
public static class NameSpaces
{
static readonly XmlSerializerNamespaces namespaces;
static NameSpaces()
{
namespaces = new XmlSerializerNamespaces();
namespaces.Add("", NameSpaces.Default);
namespaces.Add("xsi", NameSpaces.Xsi);
namespaces.Add("etd", NameSpaces.Etd);
namespaces.Add("zzu", NameSpaces.Zzu);
}
public static XmlSerializerNamespaces XmlSerializerNamespaces
{
get
{
return namespaces;
}
}
public const string Default = "http://crd.gov.pl/wzor/2014/12/08/1887/";
public const string Etd = "http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/";
public const string Xsi = "http://www.w3.org/2001/XMLSchema-instance";
public const string Zzu = "http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/10/07/eD/ORDZU/";
public const string SchemaLocation = "http://crd.gov.pl/wzor/2014/12/08/1887/ http://crd.gov.pl/wzor/2014/12/08/1887/schemat.xsd";
}
[XmlRoot("Deklaracja", Namespace = NameSpaces.Default)]
public class EPIT11V21
{
public EPIT11V21() {
this.Zalaczniki = string.Empty;
}
public EPIT11V21(int XPDeclarationID) : this()
{
this.Pouczenie = "Za uchybienie obowiązkom płatnika grozi odpowiedzialność przewidziana w Kodeksie karnym skarbowym.";
}
[XmlAttribute("schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string XSDSchemaLocation
{
get
{
return NameSpaces.SchemaLocation;
}
set {
// Do nothing - fake property.
}
}
public Podmiot1PIT11V21 Podmiot1 = new Podmiot1PIT11V21();
public String Pouczenie { get; set; }
public String Zalaczniki { get; set; }
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces xmlsn
{
get
{
return NameSpaces.XmlSerializerNamespaces;
}
set {
// Do nothing - fake property.
}
}
}
public class Podmiot1PIT11V21
{
public Podmiot1PIT11V21() { }
[XmlAttribute("rola")]
public string rola = "Płatnik";
[XmlElement("OsobaNieFizyczna", Namespace = NameSpaces.Etd)]
public OsobaNiefizycznaPIT11V21 OsobaNieFizyczna = new OsobaNiefizycznaPIT11V21();
}
public class OsobaNiefizycznaPIT11V21
{
public OsobaNiefizycznaPIT11V21() { }
public string NIP = "0000000000";
public string PelnaNazwa = "XXXXXXXX";
}
And, to test:
public static class TestClass
{
public static void Test()
{
var curpit11 = new EPIT11V21(10101);
var xml = XmlSerializationHelper.GetXml(curpit11, NameSpaces.XmlSerializerNamespaces);
Debug.WriteLine(xml);
}
}
public static class XmlSerializationHelper
{
public static string GetXml<T>(this T obj)
{
return GetXml(obj, false);
}
public static string GetXml<T>(this T obj, bool omitNamespace)
{
return GetXml(obj, new XmlSerializer(obj.GetType()), omitNamespace);
}
public static string GetXml<T>(this T obj, XmlSerializer serializer)
{
return GetXml(obj, serializer, false);
}
public static string GetXml<T>(T obj, XmlSerializer serializer, bool omitStandardNamespaces)
{
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
{
ns = new XmlSerializerNamespaces();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
}
return GetXml(obj, serializer, ns);
}
public static string GetXml<T>(T obj, XmlSerializerNamespaces ns)
{
return GetXml(obj, new XmlSerializer(obj.GetType()), ns);
}
public static string GetXml<T>(T obj, XmlSerializer serializer, XmlSerializerNamespaces ns)
{
using (var textWriter = new StringWriter())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true; // For cosmetic purposes.
settings.IndentChars = " "; // For cosmetic purposes.
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
if (ns != null)
serializer.Serialize(xmlWriter, obj, ns);
else
serializer.Serialize(xmlWriter, obj);
}
return textWriter.ToString();
}
}
}
Produces
<Deklaracja xmlns:zzu="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/10/07/eD/ORDZU/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/" xsi:schemaLocation="http://crd.gov.pl/wzor/2014/12/08/1887/ http://crd.gov.pl/wzor/2014/12/08/1887/schemat.xsd" xmlns="http://crd.gov.pl/wzor/2014/12/08/1887/">
<Podmiot1 rola="Płatnik">
<etd:OsobaNieFizyczna>
<etd:NIP>0000000000</etd:NIP>
<etd:PelnaNazwa>XXXXXXXX</etd:PelnaNazwa>
</etd:OsobaNieFizyczna>
</Podmiot1>
<Pouczenie>Za uchybienie obowiązkom płatnika grozi odpowiedzialność przewidziana w Kodeksie karnym skarbowym.</Pouczenie>
<Zalaczniki />
</Deklaracja>
I change my code like this:
Declaration curdec = gVDeclarations.GetRow(i) as Declaration;
EPIT11V21 curpit11 = new EPIT11V21(curdec.id);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("schemaLocation", #"http://crd.gov.pl/wzor/2014/12/08/1887/ http://crd.gov.pl/wzor/2014/12/08/1887/schemat.xsd");
ns.Add("xmlns", #"http://crd.gov.pl/wzor/2014/12/08/1887/");
ns.Add("etd", #"http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/");
ns.Add("xsi", #"http://www.w3.org/2001/XMLSchema-instance");
ns.Add("zzu", #"http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/10/07/eD/ORDZU/");
XmlSerializer serializer = new XmlSerializer(typeof(EPIT11V21));
using (TextWriter writer = new StreamWriter(#"F:\xml\Xml.xml"))
{
serializer.Serialize(writer, curpit11,ns);
}
and to Deklaracja sub clases
public class Podmiot1PIT11V21
{
public Podmiot1PIT11V21(){}
[XmlAttribute("rola")]
public string rola = "Płatnik";
[XmlElement("OsobaNieFizyczna", Namespace = "http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/")]
public OsobaNiefizycznaPIT11V21 OsobaNieFizyczna = new OsobaNiefizycznaPIT11V21();
}
I get almoast what iwant but 2nd line which i get:
<Deklaracja
xmlns:xmlns="http://crd.gov.pl/wzor/2014/12/08/1887/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:schemaLocation="http://crd.gov.pl/wzor/2014/12/08/1887/ http://crd.gov.pl/wzor/2014/12/08/1887/schemat.xsd"
xmlns:zzu="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/10/07/eD/ORDZU/"
xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2011/06/21/eD/DefinicjeTypy/">
is there a way to just change "ns" to change that line in easy way ?
I have the following type:
[XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/MyNamespace")]
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
public Collection<int> DataSourceIds { get; set; }
}
I'm serializing a list of Locations to XML, resulting in the following:
<ArrayOfLocation xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MyNamespace">
<Location>
<DataSourceIds xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d3p1:int>1</d3p1:int>
</DataSourceIds>
<Id>2</Id>
<Name>First</Name>
</Location>
<Location>
<DataSourceIds xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d3p1:int>1</d3p1:int>
<d3p1:int>2</d3p1:int>
<d3p1:int>3</d3p1:int>
<d3p1:int>4</d3p1:int>
</DataSourceIds>
<Id>1</Id>
<Name>Second</Name>
</Location>
</ArrayOfLocation>
I then try to deserialize this XML as follows:
var rootAttribute = new XmlRootAttribute("ArrayOfLocation")
{
Namespace = "http://schemas.datacontract.org/2004/07/MyNamespace"
};
var serializer = new XmlSerializer(typeof(Location[]), rootAttribute);
using (var xmlReader = XmlReader.Create(new StreamReader(response.GetResponseStream())))
{
locations = (Location[])serializer.Deserialize(xmlReader);
}
This returns a list of Location objects, with every property set correctly... except DataSourceIds, which remains empty. Why isn't XmlSerializer deserializing the array of integers?
Since it was serialized with DataContractSerializer, you can deserialize it like so:
[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/MyNamespace")]
public class Location
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public Collection<int> DataSourceIds { get; set; }
}
And then use it like:
using (var xmlReader = XmlReader.Create(stream))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(Location[]));
var locations = (Location[])serializer.ReadObject(xmlReader);
Debug.WriteLine(DataContractSerializerHelper.GetXml(locations, serializer)); // Debug check on re-serialization, remove when not needed.
}
The XmlRoot declaration is ignored by DataContractSerializer.
Finally, a utility method for re-serializing to an XML string, for debugging purposes:
public static class DataContractSerializerHelper
{
private static MemoryStream GenerateStreamFromString(string value)
{
return new MemoryStream(Encoding.Unicode.GetBytes(value ?? ""));
}
public static string GetXml<T>(T obj, DataContractSerializer serializer) where T : class
{
using (var textWriter = new StringWriter())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = " "; // For cosmetic purposes.
using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
{
serializer.WriteObject(xmlWriter, obj);
}
return textWriter.ToString();
}
}
public static string GetXml<T>(T obj) where T : class
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
return GetXml(obj, serializer);
}
}
I'd like to serialize the following class to xml:
public class Survey
{
[XmlElement("edit")]
public string EditLink { get; set; }
}
As expected this serializes as (removed extra stuff not important to the question)
<Survey><edit>http://example.com/editlink</edit></Survey>
However, I'd like to prepend a parent node to the edit node, so that the resultant xml is:
<Survey><links><edit>http://example.com/editlink</edit></links></Survey>
Is there a way to do this with just the serialization attributes, without modifying the structure of the class?
You can't with that structure. If you expose EditLink as a collection then you can:
public class Survey
{
[XmlArray("links")]
[XmlArrayItem("edit")]
public string[] edit
{
get
{
return new [] {EditLink};
}
set
{
EditLink = value[0];
}
}
[XmlIgnore]
public string EditLink { get; set; }
}
Which yields:
<Survey>
<links>
<edit>http://example.com/editlink</edit>
</links>
</Survey>
You can try using the XMLSerializer class.
public class Survey
{
public string EditLink { get; set; }
}
private void SerializeSurvey()
{
XmlSerializer serializer = new XmlSerializer(typeof(Survey));
Survey survey = new Survey(){EditLink=""};
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(filename, FileMode.Create);
XmlWriter writer = new XmlTextWriter(fs, Encoding.Unicode);
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, survey);
writer.Close();
}
I have this C# 4.0 type
public class DecimalField
{
public decimal Value { get; set; }
public bool Estimate { get; set; }
}
I want to use XmlSerializer to serialize the type into
<Val Estimate="true">123</Val>
Ideally, I want to omit the Estimate attribute if its value is false. Changing Estimate to a nullable bool is acceptable.
What attributes/implementations are required to go from this type to this XML representation?
Thanks.
Not sure if you can output Estimate conditionally with attributes only. But you definitelly can implement IXmlSerializable and check Estimate value inside WriteXml method.
Here is an example
Conditionally omitting Estimate would require a lof of coding. I wouldn't go that way.
XmlWriter writer = XmlWriter.Create(stream, new XmlWriterSettings() { OmitXmlDeclaration = true });
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer xml = new XmlSerializer(typeof(DecimalField));
xml.Serialize(writer, obj, ns);
-
[XmlRoot("Val")]
public class DecimalField
{
[XmlText]
public decimal Value { get; set; }
[XmlAttribute]
public bool Estimate { get; set; }
}
You can also manually serialize your class using Linq2Xml
List<XObject> list = new List<XObject>();
list.Add(new XText(obj.Value.ToString()));
if (obj.Estimate) list.Add(new XAttribute("Estimate", obj.Estimate));
XElement xElem = new XElement("Val", list.ToArray());
xElem.Save(stream);
This is about as close as you can get (an Estimate attribute is always included) without implementing IXmlSerializable:
[XmlRoot("Val")]
public class DecimalField
{
[XmlText()]
public decimal Value { get; set; }
[XmlAttribute("Estimate")]
public bool Estimate { get; set; }
}
With IXmlSerializable, your class looks like this:
[XmlRoot("Val")]
public class DecimalField : IXmlSerializable
{
public decimal Value { get; set; }
public bool Estimate { get; set; }
public void WriteXml(XmlWriter writer)
{
if (Estimate == true)
{
writer.WriteAttributeString("Estimate", Estimate.ToString());
}
writer.WriteString(Value.ToString());
}
public void ReadXml(XmlReader reader)
{
if (reader.MoveToAttribute("Estimate") && reader.ReadAttributeValue())
{
Estimate = bool.Parse(reader.Value);
}
else
{
Estimate = false;
}
reader.MoveToElement();
Value = reader.ReadElementContentAsDecimal();
}
public XmlSchema GetSchema()
{
return null;
}
}
You can test your class like this:
XmlSerializer xs = new XmlSerializer(typeof(DecimalField));
string serializedXml = null;
using (StringWriter sw = new StringWriter())
{
DecimalField df = new DecimalField() { Value = 12.0M, Estimate = false };
xs.Serialize(sw, df);
serializedXml = sw.ToString();
}
Console.WriteLine(serializedXml);
using (StringReader sr = new StringReader(serializedXml))
{
DecimalField df = (DecimalField)xs.Deserialize(sr);
Console.WriteLine(df.Estimate);
Console.WriteLine(df.Value);
}