consider following codes and classes:
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
public class Element1
{
[XmlAttribute]
public int Order { get; set; }
public string name1 { get; set; }
public ElementCollcetion collection { get; set; }
}
public class Element2
{
[XmlAttribute]
public int Order { get; set; }
public string name2 { get; set; }
}
public class Elements
{
public Element1 element1 { get; set; }
public Element2 element2 { get; set; }
}
public interface IFoo
{
string FooName { get; set; }
}
public class Foo1 : IFoo
{
public string FooName { get; set; }
public int deff1 { get; set; }
}
public class Foo2 : IFoo
{
public string FooName { get; set; }
public bool deff2 { get; set; }
}
public class ElementCollcetion : List<IFoo>, IXmlSerializable
{
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer serializer = null;
bool flag;
reader.Read();
while (true)
{
flag = false;
if (string.Compare(reader.Name, typeof(Foo1).Name) == 0)
{
serializer = new XmlSerializer(typeof(Foo1));
flag = true;
}
else if (string.Compare(reader.Name, typeof(Foo2).Name) == 0)
{
serializer = new XmlSerializer(typeof(Foo2));
flag = true;
}
if (flag)
this.Add((IFoo)serializer.Deserialize(reader));
else
break;
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
foreach (IFoo foo in this.AsEnumerable())
{
XmlSerializer serializer = new XmlSerializer(foo.GetType());
serializer.Serialize(writer, foo);
}
}
}
class Program
{
static void Main(string[] args)
{
Elements elements = new Elements()
{
element1 = new Element1
{
name1 = "Name1",
Order = 1,
collection = new ElementCollcetion(){
new Foo1{deff1=10,FooName="FooName1"},
new Foo2{deff2=true,FooName="FooName2"}
},
},
element2 = new Element2
{
name2 = "Name2",
Order = 2
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Elements));
TextWriter textWriter = new StreamWriter(#"d:\ser.xml");
serializer.Serialize(textWriter, elements);
textWriter.Close();
TextReader textReader = new StreamReader(#"d:\ser.xml");
Elements element = (Elements)serializer.Deserialize(textReader);
textReader.Close();
}
}
}
when i run it, an xml will generated into ser.xml like so:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<element1 Order="1">
<name1>Name1</name1>
<collection>
<Foo1>
<FooName>FooName1</FooName>
<deff1>10</deff1>
</Foo1>
<Foo2>
<FooName>FooName2</FooName>
<deff2>true</deff2>
</Foo2>
</collection>
</element1>
<element2 Order="2">
<name2>Name2</name2>
</element2>
</Elements>
but it cannot correctly Deserialize the file unless i reorder the elements in the xml like so:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<element2 Order="2">
<name2>Name2</name2>
</element2>
<element1 Order="1">
<name1>Name1</name1>
<collection>
<Foo1>
<FooName>FooName1</FooName>
<deff1>10</deff1>
</Foo1>
<Foo2>
<FooName>FooName2</FooName>
<deff2>true</deff2>
</Foo2>
</collection>
</element1>
</Elements>
note that serializer.UnknownAttribute and serializer.UnknownElement will not raise during both execution.
what is the problem? and how can i fix it?
---------------EDIT----------------------
i know that problem is in IXmlSerializable.ReadXml() implementation. but what kind of problem and how should i cure it?
Basically, you aren't progressing the reader to the end of the sub-tree correctly. Adding reader.Read(); to the end of ReadXml fixes it, but is a bit ugly; ReadSubtree() may be safer.
Frankly, implementing IXmlSerializable correctly and robustly is hard. I always advise against it.
Although I would have to see your IXmlSerializable implementation, my bet would be, that your ReadXml implementation has a reversed order of processing ... e.g.: it first looks for element2 instead of element1. If this is not it, please post your IXmlSerializable implementation.
EDIT
As Marc pointed out, you need to add another read. Problem was, that XmlReader by calling reader.Read() processed collection entry tag, but at the end of your method, you didn't process the end tag /collection, hence another reader.Read() call. This basically prevented the deserialization to proceed correctly.
Generally the correct pattern for ReadXml implementation is to start with:
bool isEmpty = reader.IsEmptyElement;
reader.ReadStartElement(); //Start reading the element
if (isEmpty) //Return on empty element
{
return;
}
And finish with:
reader.ReadEndElement();
Here is also your implementation using the pattern:
public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer serializer = null;
bool flag;
bool isEmpty = reader.IsEmptyElement;
reader.ReadStartElement();
if (isEmpty)
{
return;
}
while (true)
{
flag = false;
if (string.Compare(reader.Name, typeof(Foo1).Name) == 0)
{
serializer = new XmlSerializer(typeof(Foo1));
flag = true;
}
else if (string.Compare(reader.Name, typeof(Foo2).Name) == 0)
{
serializer = new XmlSerializer(typeof(Foo2));
flag = true;
}
if (flag)
this.Add((IFoo)serializer.Deserialize(reader));
else
break;
}
reader.ReadEndElement();
}
Related
I have a collection class implementing ICollection<T> with a few custom attributes thrown in for completeness..
In this simplistic sample, its a simple Request/Results pattern with the request itself being passed back as an attribute of the results class.
[Serializable]
public class MyRequest
{
public int SearchID { get; set; }
}
[Serializable]
public class MyResults : ICollection<MyElement>
{
public MyRequest RequestDetails { get; set; }
private ICollection<MyElement> _list = new List<MyElement>();
/* ICollection interface methods removed */
}
[Serializable]
public class MyElement
{
public int ID { get; set; }
}
Here's the sample program to instantiate and then output a serialized string.
class Program
{
static void Main(string[] args)
{
MyResults m = new MyResults();
m.RequestDetails = new MyRequest() { SearchID = 1 };
for (int i = 1; i <= 5; i++)
{
m.Add(new MyElement { ID = i });
}
XmlDocument xmlDoc = new XmlDocument();
XmlSerializer xmlSerializer = new XmlSerializer(m.GetType());
using (MemoryStream xmlStream = new MemoryStream())
{
xmlSerializer.Serialize(xmlStream, m);
xmlStream.Position = 0;
xmlDoc.Load(xmlStream);
}
System.Diagnostics.Debug.WriteLine(xmlDoc.OuterXml);
}
}
The problem is that the output is not including the MyRequest details...
<?xml version="1.0"?>
<ArrayOfMyElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyElement>
<ID>1</ID>
</MyElement>
<MyElement>
<ID>2</ID>
</MyElement>
<MyElement>
<ID>3</ID>
</MyElement>
<MyElement>
<ID>4</ID>
</MyElement>
<MyElement>
<ID>5</ID>
</MyElement>
</ArrayOfMyElement>
XmlSerializer always ignores extra properties when serializing a collection; the only way to do it, as far as I know, is not to implement ICollection<MyElement> on your MyResults class, and instead expose the collection as a property:
public class MyResults
{
public MyRequest RequestDetails { get; set; }
public ICollection<MyElement> Items { get; set; }
}
(the Serializable attribute isn't needed for XML serialization)
Just change ICollection to Collection because XmlSerialization does not support Generic Interfaces:
public class MyResults
{
public MyResults()
{
this.Items= new Collection<MyElement>();
}
public MyRequest RequestDetails { get; set; }
public Collection<MyElement> Items { get; set; }
}
How can I set up the System.Runtime.Serialization serializer to ignore null values?
Or do I have to use the XmlSerializer for that? If so, how?
(I don't want <ecommerceflags i:nil="true"/> tags like this to be written, if it's null then just skip it)
With System.Runtime.Serialization.DataContractSerializer you need to mark the property with [DataMember(EmitDefaultValue = false)].
Example, the code below:
class Program
{
static void Main()
{
Console.WriteLine(SerializeToString(new Person { Name = "Alex", Age = 42, NullableId = null }));
}
public static string SerializeToString<T>(T instance)
{
using (var ms = new MemoryStream())
{
var serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(ms, instance);
ms.Seek(0, SeekOrigin.Begin);
using (var sr = new StreamReader(ms))
{
return sr.ReadToEnd();
}
}
}
}
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember(EmitDefaultValue = false)]
public int? NullableId { get; set; }
}
prints the following:
<Person xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication4" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Age>42</Age>
<Name>Alex</Name>
</Person>
Though it has less value (except that it makes the serialized stream shorter), you can customize your serialization to achieve this.
When using System.Runtime.Serialization, you can implement the ISerializable interface:
[Serializable]
public class MyClass: ISerializable
{
private string stringField;
private object objectField;
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (stringField != null)
info.AddValue("str", stringField);
if (objectField != null)
info.AddValue("obj", objectField);
}
// the special constructor for deserializing
private MyClass(SerializationInfo info, StreamingContext context)
{
foreach (SerializationEntry entry in info)
{
switch (entry.Name)
{
case "str":
stringField = (string)entry.Value;
break;
case "obj":
objectField = entry.Value;
break;
}
}
}
}
When using XML serialization, you can implement the IXmlSerializable interface to customize the output in a similar way.
As far as I read, you could use the Specified-Feature
public int? Value { get; set; }
[System.Xml.Serialization.XmlIgnore]
public bool ValueSpecified { get { return this.Value != null; } }
To only write it if it's specified.
Another way would be
[System.Xml.Serialization.XmlIgnore]
private int? value;
public int Value { get { value.GetValueOrDefault(); } }
I don't know if the topic is correct, if not please correct. So far i am not sure what to search for my problem so maybe the question has already been answered before.
Currently i have the following class (as example):
[Serializable]
public class Sample
{
public string Something { get; set; }
public List<Parameter> Parameters { get; set; }
}
[Serializable]
public class Parameter
{
public string Name { get; set; }
public string Value { get; set; }
}
This structure i have to serialize to the following XML:
<Sample>
<Something>1234512345112345</Something>
<Parameters>
<Name>Value</Name>
<Name>Value</Name>
</Parameters>
</Sample>
So the XML should contain the property value of the attribute "Name" as XML-Element Name.
Update 20.05.2015
I have the following XML content:
<?xml version="1.0" encoding="utf-16" ?>
<ProcessData>
<ID>123456</ID>
<IDYTPE>BASEPLATE</IDYTPE>
<State>FAIL</State>
<Recipe>654321</Recipe>
<ProcessDataParameter>
<test_0>0</test_0>
<test_1>12,34</test_1>
<test_2>24,68</test_2>
<test_3>37,02</test_3>
<test_4>49,36</test_4>
<test_5>61,7</test_5>
</ProcessDataParameter>
</ProcessData>
When i try to use the following code to deserialize:
public void ReadXml(XmlReader reader)
{
reader.ReadStartElement("ProcessData");
this.Id = reader.ReadElementString("ID");
this.IdType = reader.ReadElementString("IDYTPE");
this.State = reader.ReadElementString("State");
this.Recipe = reader.ReadElementString("Recipe");
reader.ReadStartElement("ProcessDataParameter");
this.ProcessDataParameter = new List<ProcessDataParameter>();
var subTree = reader.ReadSubtree();
while (subTree.Read())
{
if (subTree.NodeType == XmlNodeType.Text)
{
var nm = subTree.LocalName;
//Parameters.Add(new Parameter { Name = nm, Value = subTree.Value });
}
}
reader.ReadEndElement();
}
Everything gets read out fine expect the process data parameters.
It seems like the subTree.Read() just reades the element out of the XML content instead of all elements contained in the .
In the while loop the reader goes through the following values (debuged)
test_0 (start tag)
0 (value between the tag)
test_0 (end tag
and then out of the while.
Seems like the reader sees the as an subtree.
Further only the 0 - value gets recognized as XmlNodeType.Text
You could implement IXmlSerializable and create your own custom serialization behaviour for your Sample class. So in your case something like this should work
[Serializable]
public class Sample : IXmlSerializable
{
public string Something { get; set; }
public List<Parameter> Parameters { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
XmlDocument doc = new XmlDocument();
doc.Load(reader);
Something = doc.SelectSingleNode(#"/Sample/Something").FirstChild.Value;
var parameters = doc.SelectSingleNode(#"/Sample/Parameters");
if (parameters.HasChildNodes)
{
Parameters = new List<Parameter>();
foreach (XmlElement childNode in parameters.ChildNodes)
{
Parameters.Add(new Parameter {Name = childNode.LocalName, Value = childNode.FirstChild.Value});
}
}
}
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("Something", this.Something);
writer.WriteStartElement("Parameters");
foreach (var parameter in Parameters)
{
writer.WriteElementString(parameter.Name, parameter.Value);
}
writer.WriteEndElement();
}
}
Updated to include ReadXml implementation for deserialization
I'm not quite sure if the ReadXml is complete as I can't test this now, you might have to tweak it a bit for the Parameters
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
Sample sample = new Sample(){
Something = "1234512345112345",
Parameters = new List<Parameter>(){
new Parameter(){
Name = new List<string>(){"Value", "Value"}
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(Sample));
StreamWriter writer = new StreamWriter(FILENAME);
serializer.Serialize(writer, sample);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(Sample));
XmlTextReader reader = new XmlTextReader(FILENAME);
Sample newSample = (Sample)xs.Deserialize(reader);
}
}
[XmlRoot("Sample")]
public class Sample
{
[XmlElement("Something")]
public string Something { get; set; }
[XmlElement("Parameters")]
public List<Parameter> Parameters { get; set; }
}
[XmlRoot("Parameters")]
public class Parameter
{
[XmlElement("Name")]
public List<string> Name { get; set; }
}
}
This is my first question on Stack Overflow. Apologies in advance if I don't do things quite right while I'm learning how things work here.
Here is my code :
public void TestSerialize()
{
ShoppingBag _shoppingBag = new ShoppingBag();
Fruits _fruits = new Fruits();
_fruits.testAttribute = "foo";
Fruit[] fruit = new Fruit[2];
fruit[0] = new Fruit("pineapple");
fruit[1]= new Fruit("kiwi");
_fruits.AddRange(fruit);
_shoppingBag.Items = _fruits;
Serialize<ShoppingBag>(_shoppingBag, #"C:\temp\shopping.xml");
}
public static void Serialize<T>(T objectToSerialize, string filePath) where T : class
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StreamWriter writer = new StreamWriter(filePath))
{
serializer.Serialize(writer, objectToSerialize);
}
}
[Serializable]
public class ShoppingBag
{
private Fruits _items;
public Fruits Items
{
get { return _items; }
set {_items = value; }
}
}
public class Fruits : List<Fruit>
{
public string testAttribute { get; set; }
}
[Serializable]
public class Fruit
{
public Fruit() { }
public Fruit(string value)
{
Name = value;
}
[XmlAttribute("name")]
public string Name { get; set; }
}
It produces this XML :
<?xml version="1.0" encoding="utf-8" ?>
<ShoppingBag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<Fruit name="pineapple" />
<Fruit name="kiwi" />
</Items>
</ShoppingBag>
I don't understand why I am not getting <Items testAttribute="foo">
Please can anyone tell me what I need to add to my code so that the Serializer will write this attribute out?
Thanks,
You need an intermediary class there:
class Program
{
static void Main()
{
var shoppingBag = new ShoppingBag
{
Items = new ShoppingBagItems
{
Fruits = new List<Fruit>(new[] {
new Fruit { Name = "pineapple" },
new Fruit { Name = "kiwi" },
}),
TestAttribute = "foo"
}
};
var serializer = new XmlSerializer(typeof(ShoppingBag));
serializer.Serialize(Console.Out, shoppingBag);
}
}
public class ShoppingBag
{
public ShoppingBagItems Items { get; set; }
}
public class ShoppingBagItems
{
[XmlElement("Fruit")]
public List<Fruit> Fruits { get; set; }
[XmlAttribute("testAttribute")]
public string TestAttribute { get; set; }
}
public class Fruit
{
[XmlAttribute("name")]
public string Name { get; set; }
}
Also note that you don't need to decorate your classes with the [Serializable] attribute as it is used only for binary serialization. Another remark is that you don't need to derive from List<T>, simply use it as a property.
Unfortunately, when serializing a collection the XmlSerializer doesn't take into account the extra properties of that collection. It only considers the members that implement ICollection<T>. If you want to serialize extra attributes, you need to wrap the collection in another class that is not a collection itself.
I have defined the following class:
public class Root
{
public string Name;
public string XmlString;
}
and created an object:
Root t = new Root
{ Name = "Test",
XmlString = "<Foo>bar</Foo>"
};
When I use XmlSerializer class to serialize this object, it will return the xml:
<Root>
<Name>Test</Name>
<XmlString><Foo>bar</Foo></XmlString>
</Root>
How do I make it not encode my XmlString content so that I can get the serialized xml as
<XmlString><Foo>bar</Foo></XmlString>
Thanks,
Ian
You can limit the custom serialization to just the element that needs special attention like so.
public class Root
{
public string Name;
[XmlIgnore]
public string XmlString
{
get
{
if (SerializedXmlString == null)
return "";
return SerializedXmlString.Value;
}
set
{
if (SerializedXmlString == null)
SerializedXmlString = new RawString();
SerializedXmlString.Value = value;
}
}
[XmlElement("XmlString")]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public RawString SerializedXmlString;
}
public class RawString : IXmlSerializable
{
public string Value { get; set; }
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
this.Value = reader.ReadInnerXml();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteRaw(this.Value);
}
}
You can (ab)use the IXmlSerializable interface an XmlWriter.WriteRaw for that. But as garethm pointed out you then pretty much have to write your own serialization code.
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace ConsoleApplicationCSharp
{
public class Root : IXmlSerializable
{
public string Name;
public string XmlString;
public Root() { }
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString("Name", Name);
writer.WriteStartElement("XmlString");
writer.WriteRaw(XmlString);
writer.WriteFullEndElement();
}
public void ReadXml(System.Xml.XmlReader reader) { /* ... */ }
public XmlSchema GetSchema() { return (null); }
public static void Main(string[] args)
{
Root t = new Root
{
Name = "Test",
XmlString = "<Foo>bar</Foo>"
};
System.Xml.Serialization.XmlSerializer x = new XmlSerializer(typeof(Root));
x.Serialize(Console.Out, t);
return;
}
}
}
prints
<?xml version="1.0" encoding="ibm850"?>
<Root>
<Name>Test</Name>
<XmlString><Foo>bar</Foo></XmlString>
</Root>
try this:
public class Root
{
public string Name;
public XDocument XmlString;
}
Root t = new Root
{ Name = "Test",
XmlString = XDocument.Parse("<Foo>bar</Foo>")
};
I would be very surprised if this was possible. Suppose it was possible for you to do this - what would happen if you had malformed XML in the property - everything would just break.
I expect that you will either need to write your own serialization for this case, or make it so that the XmlString field is a structure that contains a foo field.