How to deserialize xml to derived classes base on element's value? - c#

For example I have an xml:
<MyFruit>
<Fruit>
<Name>Apple</Name>
<Size>Big</Size>
</Fruit>
<Fruit>
<Name>Orange</Name>
<Price>10.00</Price>
</Fruit>
</MyFruit>
You may notice that the fruit nodes contain different elements, that's my hurt:(
Then I defined following classes in order to hold the deserialized object:
public class MyFruit
{
public List<Fruit> Fruits { get; set; }
}
public abstract class Fruit
{
public string Name { get; set; }
}
public class Apple : Fruit
{
public string Size { get; set; }
}
public class Orange : Fruit
{
public float Price { get; set; }
}
It didn't work.
I also tried:
Adding [XmlInclude(typeof (Apple))] and [XmlInclude(typeof (Orange))] attributes to the fruit base class to specify the concrete derived classes
Adding [XmlElement(typeof (Apple))] and [XmlElement(typeof (Orange)) attributes to the Fruits property of the MyFruit class
Neither of them works.
So I wonder is there a way that can control the deserialization process base on element's value(if the name is Apple, deserialize to Apple class, Orange to Orange class...), or maybe there are some better ways?
UPDATE
I wrote an extension method to deserialize xml:
public static T Deserialize<T>(this string xml)
{
if (string.IsNullOrEmpty(xml))
{
return default(T);
}
try
{
var xmlserializer = new XmlSerializer(typeof(T));
var stringReader = new StringReader(xml);
using (var reader = XmlReader.Create(stringReader))
{
return (T) xmlserializer.Deserialize(reader);
}
}
catch (Exception ex)
{
throw new Exception("反序列化发生错误", ex);
}
}

One approach is simply to transform the input xml via the XslCompiledTransform class into a format that can be easily de-serialized into the desired object structure. The following example demonstrates the concept:
// XML Deserialization helper.
class XmlSerializationHelper
{
// Transform the input xml to the desired format needed for de-serialization.
private static string TransformXml(string xmlString)
{
// XSL transformation script.
string xsl = #"<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<xsl:template match=""MyFruit"">
<xsl:element name=""{local-name()}"">
<Fruits>
<xsl:for-each select=""Fruit"">
<xsl:element name=""Fruit"">
<xsl:attribute name=""xsi:type""><xsl:value-of select=""Name""/></xsl:attribute>
<xsl:copy-of select=""./node()""/>
</xsl:element>
</xsl:for-each>
</Fruits>
</xsl:element>
</xsl:template>
</xsl:stylesheet>";
// Load input xml as XmlDocument
XmlDocument sourceXml = new XmlDocument();
sourceXml.LoadXml(xmlString);
// Create XSL transformation.
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(new XmlTextReader(new StringReader(xsl)));
// Apply transformation to input xml and write result out to target xml doc.
XmlDocument targetXml = new XmlDocument(sourceXml.CreateNavigator().NameTable);
using (XmlWriter writer = targetXml.CreateNavigator().AppendChild())
{
transform.Transform(sourceXml, writer);
}
// Return transformed xml string.
return targetXml.InnerXml;
}
public static T DeSerialize<T>(string inputXml)
{
T instance = default(T);
if (string.IsNullOrEmpty(inputXml))
return instance;
try
{
string xml = TransformXml(inputXml); // Transform the input xml to the desired xml format needed to de-serialize objects.
string attributeXml = string.Empty;
using (StringReader reader = new StringReader(xml))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (XmlReader xmlReader = new XmlTextReader(reader))
{
instance = (T)serializer.Deserialize(xmlReader);
xmlReader.Close();
}
reader.Close();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return instance;
}
}
The helper class can now be used as follow:
string inputXml = #"<MyFruit>
<Fruit>
<Name>Apple</Name>
<Size>Big</Size>
</Fruit>
<Fruit>
<Name>Orange</Name>
<Price>10.00</Price>
</Fruit>
</MyFruit>";
MyFruit fruits = XmlSerializationHelper.DeSerialize<MyFruit>(inputXml);

This one will work:
[XmlRoot("MyFruit")]
public class MyFruit : List<Fruit>
{
}
public class Fruit
{
public string Name { get; set; }
public string Size { get; set; }
public float Price { get; set; }
}
Use Deserialize from #WAKU

You can use a loop and XmlParser. Switch on the 'Name' property to parse the class specific tags. Example:
Fruit fruit;
List<Fruit> fruits;
while(true)
{
// ...
xmlReader.Read();
xmlReader.ReadStartElement("Fruit");
xmlReader.ReadStartElement("Name");
name = xmlReader.ReadString();
xmlReader.ReadEndElement();
switch(name)
{
case "apple":
fruit = new Apple();
try
{
xmlReader.ReadStartElement("weight");
(fruit as Apple).weight = Integer.Parse(xmlReader.ReadString());
xmlReader.ReadEndElement();
}catch(){}
//.....
break;
case "orange":
fruit = new Orange;
try
{
xmlReader.ReadStartElement("color");
(fruit as Orange).color = xmlReader.ReadString();
xmlReader.ReadEndElement();
}catch(){}
//.....
break;
}
xmlReader.ReadEndElement();
// ...
}

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 to create sets of the serialized objects C#

There are various types, in a special case which can be configured in different ways. How to serialize them?
[Serializable]
[XmlRoot("RootXml", Namespace = "")]
public class RootXml
{
object _schemaVersion;
[XmlElement("SchemaVersion")]
public object SchemaVersion
{
get { return _schemaVersion; }
set { _schemaVersion = value; }
}
List<object> _test;
[XmlElement("Test")]
public List<object> Test
{
get { return _test; }
set { _test = value; }
}
public RootXml()
{
}
}
I.e. root can include different objects, and they have to be serialized...
I have a xml-format approximately of such
look:
<?xml version="1.0" encoding="windows-1251"?>
<RootXml>
<SchemaVersion Number="" />
<Report Code="">
<Period Code="" Date="">
<Source ClassCode="" Code="">
<Form Code="">
<Column Num="1" Name="" />
<Column Num="2" Name="" />
<Column Num="3" Name="" />
<Document>
<Data code="11" />
<Data code="12">
<Px Num="1" Value="1" />
<Px Num="2" Value="1" />
<Px Num="4" Value="2" />
<Px Num="5" Value="2" />
</Data>
<Data code="13" />
</Document>
</Form>
</Source>
</Period>
</Report>
</RootXml>
In which some elements can change a little (Document, Document with tags, Document with the status, etc.),
included in others (for example, report incl. in scheme) ... and do not know how to change in the future.
I want to construct a set of "formats" which will also have various components, to be substituted...
Maybe for this purpose you shouldn't use serialization, and to define
set of attributes, and a reflection to process objects and to form xml (approximately just as XmlSerializer)???
You are trying to serialize and deserialize data with polymorphic fields. You have a few options here:
If you know in advance all possible types that might be encountered in a polymorphic field, you can use attributes to tell XmlSerializer how to serialize and deserialize each type. In particular, for a polymorphic field, apply [XmlElement("DerivedElementName", Type = typeof(DerivedElementType))] for every derived type that might be encountered.
For instance, simplifying your RootXml class somewhat, the following allows for two different types of report to be serialized:
[XmlRoot("Report", Namespace = "")]
public class Report
{
[XmlAttribute]
public string Code { get; set; }
[XmlElement]
public decimal TotalCost { get; set; }
}
[XmlRoot("DifferentReport", Namespace = "fuuuu")]
public class DifferentReport
{
public DifferentReport() { }
public DifferentReport(string code, string value)
{
this.Code = code;
this.Value = value;
}
[XmlAttribute]
public string Code { get; set; }
[XmlText]
public string Value { get; set; }
}
[XmlRoot("RootXml", Namespace = "")]
public class RootXml
{
public RootXml() { }
object _test;
[XmlElement("Report", Type=typeof(Report))]
[XmlElement("DifferentReport", Type = typeof(DifferentReport))]
public object Data
{
get { return _test; }
set { _test = value; }
}
}
And then later, both of the following can be serialized and deserialized:
var root1 = new RootXml { Data = new Report { Code = "a code", TotalCost = (decimal)101.01 } };
var root2 = new RootXml { Data = new DifferentReport { Code = "a different code", Value = "This is the value of the report" } };
Note that you can use the same technique with polymorphic lists, in which case the serializer will expect sequences of elements with the specified names:
[XmlRoot("RootXml", Namespace = "")]
public class RootXml
{
public RootXml() { }
List<object> _test;
[XmlElement("Report", Type=typeof(Report))]
[XmlElement("DifferentReport", Type = typeof(DifferentReport))]
public List<object> Data
{
get { return _test; }
set { _test = value; }
}
}
If the XML could be anything and you don't know what it might contain (because you must deserialize XML from the future versions and reserialize it without data loss, for example) you may need to load your XML into an XDocument then manually search for data using Linq-to-XML. For information on how to do this, see here: Basic Queries (LINQ to XML).
You could adopt a hybrid approach where you load the XML into an XDocument, then deserialize and serialize familiar portions with XmlSerializer, using the following extension methods:
public static class XObjectExtensions
{
public static T Deserialize<T>(this XContainer element)
{
return element.Deserialize<T>(new XmlSerializer(typeof(T)));
}
public static T Deserialize<T>(this XContainer element, XmlSerializer serializer)
{
using (var reader = element.CreateReader())
{
object result = serializer.Deserialize(reader);
if (result is T)
return (T)result;
}
return default(T);
}
public static XElement Serialize<T>(this T obj, bool omitStandardNamespaces = true)
{
return obj.Serialize(new XmlSerializer(obj.GetType()), omitStandardNamespaces);
}
public static XElement Serialize<T>(this T obj, XmlSerializer serializer, bool omitStandardNamespaces = true)
{
var doc = new XDocument();
using (var writer = doc.CreateWriter())
{
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
(ns = new XmlSerializerNamespaces()).Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
serializer.Serialize(writer, obj, ns);
}
return doc.Root;
}
}
Then use them to pick out and deserialize known portions of your XML as follows:
var doc = XDocument.Parse(xml);
var reportElement = doc.Root.Element("Report");
if (reportElement != null)
{
var report1 = doc.Root.Element("Report").Deserialize<Report>();
// Do something with the report.
// Create a different report
var differentReport = new DifferentReport { Code = report1.Code + " some more code", Value = "This is the value of the report" };
var differentElement = differentReport.Serialize();
reportElement.AddAfterSelf(differentElement);
reportElement.Remove();
}
OK, given that you are using c# 2.0, you can load your Xml into an XmlDocument and use it as described here: Process XML Data Using the DOM Model. This is a precursor API to Linq-to-XML and is somewhat harder to work with -- but nevertheless totally functional.
You can also adopt the hybrid approach and use XmlSerializer to deserialize and re-serialize known chunks of an XmlDocument. Here are some extension methods for this purpose -- but since you're using c# 2.0, you must remove the this keyword:
public static class XmlNodeExtensions
{
public static XmlElement SerializeToXmlElement<T>(this T o, XmlElement parent)
{
return SerializeToXmlElement(o, parent, new XmlSerializer(o.GetType()));
}
public static XmlElement SerializeToXmlElement<T>(this T o, XmlElement parent, XmlSerializer serializer)
{
int oldCount = parent.ChildNodes.Count;
XPathNavigator navigator = parent.CreateNavigator();
using (XmlWriter writer = navigator.AppendChild())
{
writer.WriteComment(""); // Kludge suggested here: https://social.msdn.microsoft.com/Forums/en-US/9ff20a3c-913d-4c6f-a18a-c10040290862/how-to-xmlserialize-directly-into-xmldocument?forum=asmxandxml
serializer.Serialize(writer, o);
}
XmlElement returnedElement = null;
for (int i = parent.ChildNodes.Count - 1; i >= oldCount; i--)
{
XmlComment comment = parent.ChildNodes[i] as XmlComment;
if (comment != null)
{
parent.RemoveChild(comment);
}
else
{
returnedElement = (parent.ChildNodes[i] as XmlElement) ?? returnedElement;
}
}
return returnedElement;
}
public static XmlDocument SerializeToXmlDocument<T>(this T o)
{
return SerializeToXmlDocument(o, new XmlSerializer(o.GetType()));
}
public static XmlDocument SerializeToXmlDocument<T>(this T o, XmlSerializer serializer)
{
XmlDocument doc = new XmlDocument();
using (XmlWriter writer = doc.CreateNavigator().AppendChild())
serializer.Serialize(writer, o);
return doc;
}
public static T Deserialize<T>(this XmlElement element)
{
return Deserialize<T>(element, new XmlSerializer(typeof(T)));
}
public static T Deserialize<T>(this XmlElement element, XmlSerializer serializer)
{
using (var reader = new XmlNodeReader(element))
return (T)serializer.Deserialize(reader);
}
}
Given those methods, you can do things like:
// Load the document from XML
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
// Find all nodes with name "Report"
foreach (XmlElement reportNode in doc.SelectNodes("/RootXml/Report"))
{
// Deserialize as a Report
Report report = XmlNodeExtensions.Deserialize<Report>(reportNode);
// Do something with it
// Create a new Report, based on the original report.
DifferentReport differentReport = new DifferentReport(report.Code + " some more code", "This is the value of the report"); ;
// Add the new report to the children of RootXml
XmlElement newNode = XmlNodeExtensions.SerializeToXmlElement(differentReport, (XmlElement)reportNode.ParentNode);
}
As you can see this is quite similar to what is possible with Linq-to-XML.

Deserialize XML with multiple elements into single C# object

I need to deserialize an XML document that looks like this:
<Root>
<Items>
<Item>
<ItemHeader Attr1="A" Attr2="B" Attr3="C" />
<ItemDetails Attr4="D" Attr5="E" />
</Item>
...
</Items>
</Root>
Into a class that looks like this:
[Serializable, XmlRoot("Item")]
Public class MyItem
{
[XmlAttribute("Attr1")]
public string Attr1 { get; set; }
[XmlAttribute("Attr5")]
public string Attr5 { get; set; }
}
And I am using the following code to perform the deserialization:
XDocument doc;
XElement rootElem = doc.Element("Root");
foreach (XElement xe in rootElem.Descendants("Item"))
{
MyItem item = new MyItem();
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyItem));
XmlReader xRdr = xe.CreateReader();
item = (MyItem)xmlSerializer.Deserialize(xRdr);
}
However, none of the elements are copied into the object instance.
Is this doable? Do I need to deserialize each sub Element?
Thanks
I'm not sure there's a way to do that using the default XML serializer via attributes, without doing the whole class structure to match your XML - so ItemHeader and ItemDetails would need their own class.
You can implement the IXmlSerializable interface though, so you can completely customize - if you must keep the structure of MyItem as it is.
static void Main(string[] args)
{
XmlSerializer myItemSerializer = new XmlSerializer(typeof(MyItem));
var xmlDoc = XDocument.Parse(#"<Item>
<ItemHeader Attr1=""A"" Attr2=""B"" Attr3=""C"" />
<ItemDetails Attr4=""D"" Attr5=""E"" />
</Item>");
using (var reader = xmlDoc.CreateReader())
{
MyItem myItem = (MyItem)myItemSerializer.Deserialize(reader);
}
Console.Read();
}
[Serializable, XmlRoot("Item")]
public class MyItem : IXmlSerializable
{
[XmlAttribute("Attr1")]
public string Attr1 { get; set; }
[XmlAttribute("Attr5")]
public string Attr5 { get; set; }
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
reader.ReadStartElement("Item");
do
{
switch (reader.Name)
{
case "ItemHeader":
Attr1 = reader.GetAttribute("Attr1");
reader.Read();
break;
case "ItemDetails":
Attr5 = reader.GetAttribute("Attr5");
reader.Read();
break;
default:
throw new XmlException(String.Format("{0} was not expected", reader.Name));
}
} while (reader.Name != "Item");
reader.ReadEndElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteStartElement("ItemHeader");
writer.WriteAttributeString("Attr1", Attr1);
writer.WriteEndElement();
writer.WriteStartElement("ItemDetails");
writer.WriteAttributeString("Attr5", Attr5);
writer.WriteEndElement();
}
}
The issue is the xml data you are receiving does not match the serialization attributes of MyItem, schemas differ. There are multiple ways around this but I guess the quickest and dirtiest solution would be to extract the part that interests you:
XDocument doc = XDocument.Load (#"Your.xml");
XElement rootElem = doc.Element("Root");
XElement itemsElem = rootElem.Element("Items");
foreach (XElement xe in itemsElem.Elements("Item"))
{
MyItem item = new MyItem()
{
Attr1 = xe.Element("ItemHeader").Attribute("Attr1").Value,
Attr5 = xe.Element("ItemDetails").Attribute("Attr5").Value
};
}

how do i deserialize this xml?

I need to deserialize the xml below. I want to use xmlserializer because I am (more) familiar with it.
I believe this xml is not constructed correctly however I cannot change it.
The below represents a list of category objects. When I try to deserialize using
xmlserializer(typeof(List<Category>))
I get this error: "categories xmlns='' is not expected"
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category id="16" name="Exports" parent_id="13"/>
<category id="17" name="Imports" parent_id="13"/>
<category id="3000" name="Income Payments & Receipts" parent_id="13"/>
<category id="125" name="Trade Balance" parent_id="13"/>
<category id="127" name="U.S. International Finance" parent_id="13"/>
</categories>
I don't mind making some kind of dummy class to deserilize these if that is what I have to do.
Here is my Category Class
[XmlType("category")]
public class Category
{
[XmlAttribute("id")]
public int ID { get; set; }
[XmlAttribute("parent_id")]
public int ParentID { get; set; }
[XmlAttribute("name")]
public string Name { get; set; }
}
My code:
XmlSerializer serializer = new XmlSerializer(typeof(List<Category>));
StringReader reader = new StringReader(xml);
List<Category> obj = null;
using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader))
{
obj = (List<Category>)serializer.Deserialize(xmlReader);
}
return obj;
You can just pass in the XmlRootAttribute into the serializer for the "categories" part.
BUT... you must remove the "&" from your xml because its not valid
XmlSerializer serializer = new XmlSerializer(typeof(List<Category>), new XmlRootAttribute("categories"));
using (FileStream fileStream = new FileStream(#"C:\Test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
var test = serializer.Deserialize(fileStream);
}
Here is your method working with a String.Replace to sort out the "&"
private List<Category> GetCategories(string xmlData)
{
List<Category> obj = null;
XmlSerializer serializer = new XmlSerializer(typeof(List<Category>), new XmlRootAttribute("categories"));
StringReader reader = new StringReader(xmlData.Replace("&","&"));
using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader))
{
obj = (List<Category>)serializer.Deserialize(xmlReader);
}
return obj;
}
Try to make a categories class that will contain your List<Category> like this:
[XmlRoot("categories")]
public class Categories
{
public Categories()
{
Items = new List<User>();
}
[XmlElement("category")]
public List<Category> Items {get;set;}
}
You can than create a serializer like this:
XmlSerializer serializer = new XmlSerializer(typeof(Categories));
Do you have an XSD that this XML should conform to? If so, you can generate the required code using:
"xsd your.xsd /classes"

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