xsi and xsd namespaces missing when using custom IXmlSerializable - c#

I am encountering the following problem. Whenever I use the default XML serialization in my C# class, the namespaces xsi and xsd are automatically added by the .NET serialization engine. However when the serialization is defined through IXmlSerializable, the namespaces are not added.
Example: this code:
class Program
{
static void Main(string[] args)
{
OutputSerialized(new Outer() { Inner = new Inner() });
OutputSerialized(new OuterCustom() { Inner = new Inner() });
}
static void OutputSerialized<T>(T t)
{
var sb = new StringBuilder();
using (var textwriter = new StringWriter(sb))
new XmlSerializer(typeof(T)).Serialize(textwriter, t);
Console.WriteLine(sb.ToString());
}
}
[Serializable] public class Inner { }
[Serializable] public class Outer { public Inner Inner { get; set; } }
public class OuterCustom : IXmlSerializable
{
public Inner Inner;
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteStartElement("Inner");
new XmlSerializer(typeof(Inner)).Serialize(writer, Inner);
writer.WriteEndElement();
}
public System.Xml.Schema.XmlSchema GetSchema() { return null; }
public void ReadXml(System.Xml.XmlReader reader) { /**/ }
}
produces the following output:
<?xml version="1.0" encoding="utf-16"?>
<Outer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Inner />
</Outer>
<?xml version="1.0" encoding="utf-16"?>
<OuterCustom>
<Inner>
<Inner xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
</Inner>
</OuterCustom>
You can see that the OuterCustom's serialized form is missing the xsd and xsi namespaces.
How can I make my OuterCustom behave the same way as Outer? Am I missing something in my code? Am I doing the custom serialization wrong?
There are a lot of questions here on SO about how to get rid of the additional namespaces, but it seems that no one asked about how to get them back.

First of all - there is no difference between mentioned versions of XML for compliant XML parser. It does not really matter if/how/where namespace prefixes defined as long as nodes have correct namespaces associated.
As this question is explicitly about style where to put namespace declaration one makes own call what is nice and what is not. In this particular case goal is to avoid duplicated xmlns attributes and move them higher in the tree.
One can write xmlns attributes wherever needed as long they don't conflict on the same node. Use XmlWriter.WriteAttributeString method to add them:
writer.WriteAttributeString(
"xmlns", "prefix", null, "urn:mynamespace");
To avoid duplicate declarations - check if prefix already defined for given namespace using XmlWriter.LookupPrefix
Sample code to ensure xsd and xsi prefixes (by Vlad):
const string xsiNamespace = System.Xml.Schema.XmlSchema.InstanceNamespace;
const string xsdNamespace = System.Xml.Schema.XmlSchema.Namespace;
public static void EnsureDefaultNamespaces(System.Xml.XmlWriter writer)
{
if (writer.LookupPrefix(xsiNamespace) == null)
writer.WriteAttributeString("xmlns", "xsi", null, xsiNamespace);
if (writer.LookupPrefix(xsdNamespace) == null)
writer.WriteAttributeString("xmlns", "xsd", null, xsdNamespace);
}
Alternative approach if more changes desired to achieve nice looking XML is to allow serialization to finish and than process XML to adjust prefixes definitions (i.e. normalize to desired prefixes, collect all namespaces and define all on top). One can either read XML with C# code or even create XSLT transformation.

Related

Getting information from my XML

I'm quite new to XML serialization/deserialization and I'm wondering how I should apply tags (XmlElement, XmlAttribute etc) to the following objects which I'm writing to XML files, to make it most optimal for me to later use LINQ or similar to get out the data I want. Let me give an example:
[XmlRoot("Root")]
public class RootObject
{
public Services Services { get; set; }
}
public class Services
{
public Service TileMapService { get; set; }
}
public class Service
{
public string Title { get; set; }
public string href { get; set; }
}
Here I've defined some properties which I'm going to write to XML with some values I'm gonna add later. At this point, I've hardcoded in the values in this method:
public static void RootCapabilities()
{
RootObject ro = new RootObject()
{
Services = new Services()
{
TileMapService = new Service()
{
Title = "Title",
href = "http://something"
}
}
};
Which gets me this XML output:
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Services>
<TileMapService>
<Title>Title</Title>
<href>http://something</href>
</TileMapService>
</Services>
</Root>
My question is if this is a valid way of doing it or if I have to use the 'XmlElement' and 'XmlAttribute' tags to later deserialize this XML file and get the information out of it, that I want (for example 'title').
I haven't been sure how to write this question, so please let me know if it's too vague.
I would not use XML serialization if I were you. Any changes to your schema/object structure and you immediately lose backwards compatibility due to how XML serialization works as a technology.
Rather separate out the XML serialization from the actual class structure - this way you can make changes to the XML/object schema and write proper migrating functions to handle any changes you make in the future.
Rather use the LINQ-to-XML classes to construct your XML document and save it as they are the easiest way in .NET to convert object structures to XML in a decoupled manner.
if the output is what you expect I think that you shouldn't change anything.
You should use Decorators like XmlAttribute or XmlIgnore when you want change the default behavior (e.g don't include a field, include one field as an attribute...)
Their role is obtain full control of the serialization and avoid unexpected behaviors
If you don't want to worry about the nitty-gritty of the serialization, XmlSerializer has always treated me well.
public static void SerializeObject(T obj, string file)
{
XmlSerializer s = new XmlSerializer(typeof(T));
TextWriter w = new StreamWriter(file);
s.Serialize(w, obj);
w.Close();
}
public static T DeserializeObject(string file)
{
XmlSerializer s = new XmlSerializer(typeof(T));
using (StreamReader r = new StreamReader(file))
{
return (T)s.Deserialize(r);
}
}
This should really only be used with references types though (objects, not primitive types or structs). Deserialize can return null and casting that to a value type will bring nothing but heartache.

how to serialize class objects to xml document

I hope to find a solution from you. What I need is to serialize ValidatorList class object into an xml document. How to do this?
I tried like this:
XmlSerializer _serializer = new XmlSerializer(list);
But I don't know how to make output of xml for list that has several classes.
C#
_list= new ListVal();
Type _type = typeof(ras);
_list.Add(new RequiredField
{
Property = _type.GetProperty("CustRef")
}.Add(new AsciiVal()));
_list.Add(new RequiredField
{
Property = _type.GetProperty("ctr")
}.Add(new StringLengthVal
{
min= 3,
max= 3
}));
[Serializable]
public class Field
{
public Field Next
{
get;
set;
}
public Field TypeName
{
get;
set;
}
public Field PropertyName
{
get;
set;
}
}
public class RequiredField : Field
{
//TODO
}
public class AsciiVal: Field
{
//TODO
}
public class StringLengthVal: Field
{
//TODO
}
public class ListVal: List<Field>
{
//TODO
}
I have something close, but not exactly the Xml you want. In actual fact I think you'll see that the Xml produced below makes a bit more sense than what you have.
To get you started, you control the serialization and deserialization using attributes in the System.Xml.Serialization namespace. A few useful ones to read up on are
XmlRootAttribute
XmlElementAttribute
XmlAttributeAttribute
XmlIncludeAttribute
So I mocked up some code which closely matches your own. Notice the addition of some attributes to instruct the serializer how I want the Xml to be laid out.
[XmlInclude(typeof(AsciiValidator))]
[XmlInclude(typeof(RequiredValidator))]
[XmlInclude(typeof(StringLengthValidator))]
public class FieldValidator
{
[XmlElement("Next")]
public FieldValidator Next
{
get;
set;
}
[XmlElement("PropertyName")]
public string PropertyName
{
get;
set;
}
}
public class AsciiValidator: FieldValidator
{
}
public class RequiredValidator: FieldValidator
{
}
public class StringLengthValidator: FieldValidator
{
[XmlElement]
public int MinLength{get;set;}
[XmlElement]
public int MaxLength{get;set;}
}
[XmlRoot("ValidatorList")]
public class ValidatorList : List<FieldValidator>
{
}
Point of interest; Every class inheriting FieldValidator must be added to the list of known types using XmlIncludeAttribute so the serializer knows what to do with them)
Then I created an example object map:
var test = new ValidatorList();
test.Add(
new RequiredValidator()
{
PropertyName="CustRef",
Next = new AsciiValidator()
});
test.Add(
new RequiredValidator()
{
PropertyName="CurrencyIndicator",
Next = new StringLengthValidator(){
MinLength=3,
MaxLength = 10
}
});
Finally I told the serializer to serialize it (and output the result to the console)
var ser = new XmlSerializer(typeof(ValidatorList));
ser.Serialize(Console.Out,test);
This was the result:
<?xml version="1.0" encoding="utf-8"?>
<ValidatorList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FieldValidator xsi:type="RequiredValidator">
<Next xsi:type="AsciiValidator" />
<PropertyName>CustRef</PropertyName>
</FieldValidator>
<FieldValidator xsi:type="RequiredValidator">
<Next xsi:type="StringLengthValidator">
<MinLength>3</MinLength>
<MaxLength>10</MaxLength>
</Next>
<PropertyName>CurrencyIndicator</PropertyName>
</FieldValidator>
</ValidatorList>
Not a million miles away from what you wanted. There is the need to output certain things in a certain way (eg xsi:type tells the serializer how to deserialize back to the object map). I hope this gives you a good start.
Here is a live, working example: http://rextester.com/OXPOB95358
Deserialization can be done by calling the Deserialize method on the XmlSerializer.
For example, if your xml is in a string:
var ser = new XmlSerializer(typeof(ValidatorList));
var test = "<..../>" // Your Xml
var xmlReader = XmlReader.Create(new StringReader(test));
var validatorList = (ValidatorList)ser.Deserialize(xmlReader);
There are many overrides of Deserialize which take differing inputs depending if the data is in a Stream an existing reader, or saved to a file.
You have to decorate the class that contains the _validators field with the KonwnType attribute
[Serializable]
[KwownType(typeof(RequiredFieldValidator)]
[KwownType(typeof(AsciValidator)]
public class MySerialisableClass
I have several SO answers that detail how to serialize objects using XML. I'll provide links below.
However, since you're looking for a rather simple serialization of your object, you may want to read up on the DataContractSerializer. It's much less complicated than the old .NET 1.x XML Serialization.
Here's the list of SO answers:
Omitting all xsi and xsd namespaces when serializing an object in .NET?
XmlSerializer: remove unnecessary xsi and xsd namespaces
Suppress xsi:nil but still show Empty Element when Serializing in .Net
Using XmlAttributeOverrides on Nested Properties
Even though many of these have to do with XML serialization and namespaces, they contain complete examples of serializing an object to XML using .NET 1.x XML Serialization.

How to create XML with multiple namespace attributes in C#

How can I generate, for example, this XML in C#
<?xml version='1.0'?>
<oneshot xmlns='http://www.w3.org/2002/xforms' xmlns:dm='http://mobileforms.foo.com/xforms' xmlns:h='http://www.w3.org/1999/xhtml' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<dm:form_namespace>Foo</dm:form_namespace>
<Days>6</Days>
<Leave_Type>Option 3</Leave_Type>
</oneshot>
I'm specifically struggling with the xmlns:dm declaration. Any ideas?
Your best bet (read: minimal amount of hacks) is probably going to be a custom IXmlSerializable implementation; you can get part-way to what you want via combinations of XmlRootAttribute, XmlElementAttribute, etc, like so:
[Serializable]
[XmlRoot("oneshot")]
public class OneShot
{
[XmlElement("form_namespace", Namespace="http://mobileforms.foo.com/xforms")]
public string FormNamespace {get; set;}
[XmlElement("Days")]
public int Days {get; set;}
[XmlElement("Leave_Type")]
public string LeaveType {get; set;}
Which will generate something like:
<?xml version="1.0" encoding="utf-16"?>
<oneshot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<form_namespace xmlns="http://mobileforms.foo.com/xforms">Foo</form_namespace>
<Days>6</Days>
<Leave_Type>Option 3</Leave_Type>
</oneshot>
But if you implement IXmlSerializable, you have full control:
public class OneShot : IXmlSerializable
{
public string FormNamespace {get; set;}
public int Days {get; set;}
public string LeaveType {get; set;}
#region IXmlSerializable
public void WriteXml (XmlWriter writer)
{
writer.WriteStartElement("oneshot");
writer.WriteAttributeString("xmlns", null, "http://www.w3.org/2002/xforms");
writer.WriteAttributeString("xmlns:dm", null, "http://mobileforms.foo.com/xforms");
writer.WriteAttributeString("xmlns:h", null, "http://www.w3.org/1999/xhtml");
writer.WriteAttributeString("xmlns:xsd", null, "http://www.w3.org/2001/XMLSchema");
writer.WriteElementString("dm:form_namespace", null, FormNamespace);
writer.WriteElementString("Days", Days.ToString());
writer.WriteElementString("Leave_Type", LeaveType);
writer.WriteEndElement();
}
public void ReadXml (XmlReader reader)
{
// populate from xml blob
}
public XmlSchema GetSchema()
{
return(null);
}
#endregion
}
Which gives you:
<?xml version="1.0" encoding="utf-16"?>
<OneShot>
<oneshot xmlns="http://www.w3.org/2002/xforms" xmlns:dm="http://mobileforms.foo.com/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<dm:form_namespace>Foo</dm:form_namespace>
<Days>6</Days>
<Leave_Type>Option 3</Leave_Type>
</oneshot>
</OneShot>
One way to write XML with nodes in different namespaces is to use 4-argument version of XmlWriter.WriteElementString to explicitly specify namespace and prefixes the way you want:
var s = new StringWriter();
using (var writer = XmlWriter.Create(s))
{
writer.WriteStartElement("oneshot", "http://www.w3.org/2002/xforms");
writer.WriteElementString("dm", "form_namespace",
"http://mobileforms.foo.com/xforms","Foo");
// pick "http://www.w3.org/2002/xforms" by default for Days node
writer.WriteElementString("Days", "6");
// or you can explicitly specify "http://www.w3.org/2002/xforms"
writer.WriteElementString("Leave_Type",
"http://www.w3.org/2002/xforms", "Option 3");
writer.WriteEndElement();
}
Console.Write(s.ToString());
Note that your sample XML defines more prefixes than are used in the XML. If your requirement is to produce "text identical XML" (vs. identical from XML point of view, but not necessary represented with identical text) you may need to put more effort in adding namespace prefixes and xmlns attributes in places you need.
Note 2: creating XML object first (XDocument for modern/LINQ way, or XmlDocument if you like DOM more) may be easier approach.
Try using the XAML serializer from .NET 4.0+'s assembly System.Xaml.
You may need to add attributes to mark properties as content instead of XML attributes.
Thanks, all, for the help! It was a combination of two of the above answers that did it for me. I'm going to set the one that suggested the IXmlSerializable approach as that was the majority of the solution.
I had to declare the XMLRoot tag on the class name, remove WriteStartElement and WriteEndElement, and then use the four parameter declaration.
This is the class that worked in the end:
[Serializable]
[XmlRoot("oneshot")]
public class LeaveRequestPush : IXmlSerializable
{
public string FormNamespace { get; set; }
public int Days { get; set; }
public string LeaveType { get; set; }
#region IXmlSerializable
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("dm", "form_namespace", "http://mobileforms.devicemagic.com/xforms", FormNamespace);
writer.WriteElementString("Days", Days.ToString());
writer.WriteElementString("Leave_Type", LeaveType);
}
}
public void ReadXml (XmlReader reader)
{
// populate from xml blob
}
public XmlSchema GetSchema()
{
return(null);
}
Again, thanks all for the combined efforts. I would not have got this one by myself!
This code does the trick!
public void WriteXml(XmlWriter writer)
{
const string ns1 = "http://firstline.com/";
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";
writer.WriteStartElement("myRoot", ns1);
writer.WriteAttributeString("SchemaVersion", "1.0");
writer.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", xsi);
writer.WriteAttributeString("xsi", "schemaLocation", xsi, ns1 + " schema1.xs");
writer.WriteStartElement("element1", ns1);
writer.WriteElementString("test1", ns1, "test value");
writer.WriteElementString("test2", ns1, "value 2");
writer.WriteEndElement();
writer.WriteEndElement();//to close classname that has root xml
}
Xml with multiple awesome namespaces!
<?xml version="1.0" encoding="utf-16"?>
<myClassNameWhereIXmlSerializableIsImplemented>
<myRoot SchemaVersion="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://firstline.com/ schema1.xs" xmlns="http://firstline.com/">
<element1>
<test1>test value</test1>
<test2>value 2</test2>
</element1>
</myRoot>
</myClassNameWhereIXmlSerializableIsImplemented>
There is something confusing about this line
writer.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", xsi);
if you give a random url instead of "http://www.w3.org/2000/xmlns/" , it will fail. The url that appear in xml is actually from "xsi" variable.
One more example to prove the point
writer.WriteAttributeString("xml", "base", "http://www.w3.org/XML/1998/namespace", base1);
where base1 = "<custom url>"
and if you want to output with a prefix like this <d:test1>somevalue<\d:test1> then writer.WriteElementString("d","test1", ns1, "somevalue"); if ns1 is not defined above then it will be added in xml output.
but for
<d:test1>
<blah>...
<\d:test1>
StartElement is needed writer.WriteStartElement("test1", ns1); to be closed when needed with writer.WriteEndElement();

XML serialization results in duplicate nodes

I have an object structure that I'm trying to serialize to xml that's resulting in a duplicated node level. I'm pretty sure it has something to do with subclassing because I had to implement my own deserialization but I'm not sure exactly what's going on in the other direction. The same xml structure is used as input when deserializing my data at application launch as when reserializing it to be saved later.
Here's what the faulty output xml looks like:
<Keyboarding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Exercises>
<Exercise>
<Exercise Id="3" ActivityNumber="5" SubActivityNumber="b" Type="Standard">
<Title>Test Title</Title>
<Instructions>Downloaded Update Instructions</Instructions>
</Exercise>
</Exercise>
</Exercises>
</Keyboarding>
Here's what it should look like (and looks like on initial deserialization):
<Keyboarding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Exercises>
<Exercise Id="3" ActivityNumber="5" SubActivityNumber="b" Type="Standard">
<Title>Test Title</Title>
<Instructions>Downloaded Update Instructions</Instructions>
</Exercise>
</Exercises>
</Keyboarding>
The root object contains a collection of exercises. Exercise is my base class, while subclasses are determined by the Type attribute. I'm using a custom xml serializer to build the objects of varying derived types from Exercise because it allows me to use reflection to match any of the 2 dozen or so potential derived types, which are in an unknown order and quantity when first read in by my application.
Here's the root element's collection, using the custom xml serializer:
[XmlArray("Exercises")]
[XmlArrayItem("Exercise", Type = typeof (ExerciseXmlSerializer<Exercise>))]
public Collection<Exercise> UnprocessedExercises { get; set; }
My base exercise class is declared as follows:
[Serializable]
[XmlType(AnonymousType = true)]
public class Exercise
And my derived class is declared as follows:
[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "Exercise", IsNullable = false)]
public class StandardExercise : Exercise
Here's the writer portion of my custom xml serializer:
public class ExerciseXmlSerializer<T> : IXmlSerializable where T : class
{
private T _data;
...
public void WriteXml(XmlWriter writer)
{
Type type = _data.GetType();
new XmlSerializer(type).Serialize(writer, _data);
}
...
}
In the WriteXml() method, the type variable is properly set to the derived type, so why does it create a node level for both the base type and the derived type? How do I prevent it from doing so?
I think your problem is with this line:
[XmlArrayItem("Exercise", Type = typeof (ExerciseXmlSerializer<Exercise>))]
I don't think that you want to indicate that the type of the items are a type of the serializer. If you want to implement IXmlSerializable, it needs to be on the class to be serialized.
Here is what I got to work:
public class Keyboarding
{
[XmlArray("Exercises")]
[XmlArrayItem("Exercise")]
public Collection<Exercise> UnprocessedExercises { get; set; }
public Keyboarding()
{
UnprocessedExercises = new Collection<Exercise>();
UnprocessedExercises.Add(new StandardExercise());
}
}
[XmlInclude(typeof(StandardExercise))]
public class Exercise
{
}
[Serializable]
public class StandardExercise : Exercise
{
}
This produces output similar to the following:
<?xml version=\"1.0\"?>
<Keyboarding xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<Exercises>
<Exercise xsi:type=\"StandardExercise\" />
</Exercises>
</Keyboarding>
If you don't want the xsi:type included in the output, you can use the answer to this question.
You should be able to use XmlAttributeOverrides to do what you're asking. In this case, you'd probably do something like this. The sample code is a little rough because you didn't include your entire object graph, but hopefully this will point you in the right direction.
List<Type> extraTypes = new List<Type>();
XmlAttributes attrs = new XmlAttributes();
extraTypes.Add(typeof(Exercise));
extraTypes.Add(typeof(StandardExercise));
XmlElementAttribute attr = new XmlElementAttribute
{
ElementName = "Exercise",
Type = typeof(Exercise)
};
attrs.XmlElements.Add(attr);
attr = new XmlElementAttribute
{
ElementName = "StandardExercise",
Type = typeof(StandardExercise)
};
attrs.XmlElements.Add(attr);
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
overrides.Add(typeof(Keyboarding), "Keboarding", attrs);
return new XmlSerializer(typeof(Keyboarding), overrides, extraTypes.ToArray(),
new XmlRootAttribute("Keyboarding"), string.Empty);
This should allow you to create an XML tree that looks like this. It's not quite what you were looking for, but is just as descriptive.
<Keyboarding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Exercises>
<StandardExercise Id="3" ActivityNumber="5" SubActivityNumber="b">
<Title>Test Title</Title>
<Instructions>Downloaded Update Instructions</Instructions>
</StandardExercise >
</Exercises>
</Keyboarding>

Wrapping Serialized Properties in Additional Elements with .NET XML Serialization

I am using C# + VSTS2008 + .Net 3.0 to do XML serialization. The code works fine. Here below is my code and current serialized XML results.
Now I want to add two additional layers to the output XML file. Here is my expected XML results. Any easy way to do this? I am not sure whether NestingLevel could help to do this. I want to find an easy way which does not change the structure of MyClass and MyObject.
Expected XML serialization result,
<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyObjectProperty>
<AdditionalLayer1>
<AdditionalLayer2>
<ObjectName>Foo</ObjectName>
</AdditionalLayer1>
</AdditionalLayer2>
</MyObjectProperty>
</MyClass>
Current XML serialization result,
<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyObjectProperty>
<ObjectName>Foo</ObjectName>
</MyObjectProperty>
</MyClass>
My current code,
public class MyClass
{
public MyObject MyObjectProperty;
}
public class MyObject
{
public string ObjectName;
}
public class Program
{
static void Main(string[] args)
{
XmlSerializer s = new XmlSerializer(typeof(MyClass));
FileStream fs = new FileStream("foo.xml", FileMode.Create);
MyClass instance = new MyClass();
instance.MyObjectProperty = new MyObject();
instance.MyObjectProperty.ObjectName = "Foo";
s.Serialize(fs, instance);
return;
}
}
I want to find an easy way which does not change the structure of MyClass and MyObject.
Frankly, that isn't going to happen. XmlSerializer (when used simply) follows the structure of the classes / members. So you can't add extra layers without changing the structure.
When used in a bespoke way (IXmlSerializable), the one thing you can say for sure is that it isn't simple... this is not a fun interface to implement.
My advice: introduce a DTO that mimics your desired format, and shim to that before you serialize.
Why do you need these additional two layers?
Is its a business requirement, you should add these two objects to your object model:
public class MyClass
{
public AdditionalLayer1 AdditionalLayer1;
}
public class AdditionalLayer1
{
public AdditionalLayer2 AdditionalLayer2;
}
public class AdditionalLayer2
{
public MyObject MyObjectProperty;
}
public class MyObject
{
public string ObjectName;
}
if its purely because of some compatibility requirements you can either do the thing above, or implement IXmlSerializable on MyClass:
public class MyClass : IXmlSerializable
{
public MyObject MyObjectProperty;
public void WriteXml (XmlWriter writer)
{
//open the two layers
//serialize MyObject
//close the two layers
}
public void ReadXml (XmlReader reader)
{
//read all the layers back, etc
}
public XmlSchema GetSchema()
{
return(null);
}
}
You could do a transformation with XSL after serializing and add the "missing" tags.
I would suggest serializing out to a MemoryStream instead of a FileStream and then loading the XML into an XmlDocument, then using the methods of that class to insert the extra nodes you want. After that, you can save it out.
I did exactly this only recently and didn't find overriding the IXmlSerializable interface at all difficult or complicated. Serialize/deserialize as normal but for the class that contains the sub-class which you need to wrap in extra tags derive from IXmlSerializable:
class ContainingClass : IXmlSerializable
{
...
#region IXmlSerializable Members
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
**reader.ReadStartElement("Equipment");**
XmlSerializer ser = new XmlSerializer(typeof(Host));
while (reader.NodeType != XmlNodeType.EndElement)
{
Host newHost = new Host();
newHost.Name = (string) ser.Deserialize(reader);
_hosts.Add(newHost);
reader.Read();
}
// Read the node to its end.
// Next call of the reader methods will crash if not called.
**reader.ReadEndElement(); // "Equipment"**
}
public void WriteXml(XmlWriter writer)
{
XmlSerializer ser = new XmlSerializer(typeof(Host));
**writer.WriteStartElement("Equipment");**
foreach (Host host in this._hosts)
{
ser.Serialize(writer, host);
}
**writer.WriteEndElement();**
}
#endregion
All I do here is to wrap the de/serialization of the lower level objects. Here I'm wrapping an extra tag "Equipment" around all the Hosts in a collection in this class. You could add as many tags as you like here as long as you close each one you start.
NOTE: Bold is showing as ** text ** - just make sure you get rid of the double asterisks :)

Categories