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);
}
}
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 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 ?
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.
This is my Model class:
public class ModelClass
{
public ModelClass()
{
}
public ModelClass(string operationName)
{
OperationName = operationName;
}
public string OperationName { get; set; }
public int Count { get; set; }
}
I have to serialize a list of this model class to a database table:
The table schema is:
ModelObjectContent (the serialized string)
IDModel
The method wich I use to serialize:
public static string SerializeObject(this List<ModelClass> toSerialize)
{
var xmlSerializer = new XmlSerializer(toSerialize.GetType());
var textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
When I save it to the database the string generated is like this:
<ArrayOfChartModel>
<ModelClass>
<OperationName>Chart1</OperationName>
<Count >1</Count>
</ModelClass>
<ModelClass>
<OperationName>Chart2</OperationName>
<Count >2</Count>
</ModelClass>
//etc
</ArrayOfChartModel>
The framework automattically creates a tag named ArrayOfChartModel and thats ok,
but my question is:
How to deserealize this xml to a list of ModelClass again?
This should work for you:
public static List<ModelClass> Deserialize(string xml)
{
var xmlSerializer = new XmlSerializer(typeof(ModelClass));
using (TextReader reader = new StringReader(xml))
{
return(List<ModelClass>)xmlSerializer.Deserialize(reader);
}
}
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);
}