XML attribute not getting namespace prefix - c#

I need to generate the following XML during serialization:
(fragment)
<IncidentEvent a:EventTypeText="Beginning" xmlns:a="http://foo">
<EventDate>2013-12-18</EventDate>
<EventTime>00:15:28</EventTime>
</IncidentEvent>
The class in question looks like this:
public class IncidentEvent
{
public string EventDate { get; set; }
public string EventTime { get; set; }
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
It appears that the serializer is noticing that the namespace is already declared in an xmlns: at the root and is ignoring my attribute. I also tried the following:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
public string EventDate { get; set; }
public string EventTime { get; set; }
private XmlSerializerNamespaces _Xmlns;
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Xmlns
{
get
{
if (_Xmlns == null)
{
_Xmlns = new XmlSerializerNamespaces();
_Xmlns.Add("ett", "http://foo");
}
return _Xmlns;
}
set
{
_Xmlns = value;
}
}
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
This results in the following XML:
<ett:IncidentEvent EventTypeText="Beginning" xmlns:ett="http://foo">
<ett:EventDate>2013-12-18</ett:EventDate>
<ett:EventTime>00:15:28</ett:EventTime>
</ett:IncidentEvent>
Which is not what I want. The element shouldn't be prefixed, the attribute should be. What is needed to get the serializer to understand what I want?

This may be a bug in XmlSerializer.
As you have noticed, even when XmlAttributeAttribute.Namespace is set explicitly, the attribute will not be prefixed in certain situations. From testing, this appears to happen when the attribute namespace happens to be the same as the namespace of the element currently being written.
For instance:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
Serializes to the following XML:
<q1:IncidentEvent EventTypeText="an attribute" xmlns:q1="http://foo" />
And since the attribute is unprefixed, it's actually not in any namespace, as is explained in the XML standard: The namespace name for an unprefixed attribute name always has no value.
However, the following:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
[XmlAttribute("EventTypeText", Namespace = "http://bar")]
public string EventTypeText { get; set; }
}
Serializes with the attribute correctly prefixed:
<q1:IncidentEvent p1:EventTypeText="an attribute" xmlns:p1="http://bar" xmlns:q1="http://foo" />
The workaround is to explicitly set [XmlAttribute(Form = XmlSchemaForm.Qualified)]. Thus:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
[XmlAttribute("EventTypeText", Namespace = "http://foo", Form = XmlSchemaForm.Qualified)]
public string EventTypeText { get; set; }
}
Serializes to
<q1:IncidentEvent q1:EventTypeText="an attribute" xmlns:q1="http://foo" />
as required.

I did some research may be following answer helps
For Attributes to have namespace prefix you have to specify a different namespace tag other than what you have specified http://foo. Following code hopefully will solve your issue. In the code i have remove the namespace for elements and added only for the attribute.
public class IncidentEvent
{
public string EventDate { get; set; }
public string EventTime { get; set; }
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
class Program
{
static void Main(string[] args)
{
IncidentEvent xmlObj = new IncidentEvent()
{
EventDate = "2012.12.01",
EventTime = "1:00:00",
EventTypeText = "Beginining"
};
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("ett", "http://foo");
XmlSerializer serializer = new XmlSerializer(typeof(IncidentEvent));
serializer.Serialize(Console.OpenStandardOutput(), xmlObj, ns);
Console.WriteLine();
}
}
http://www.w3.org/TR/2009/REC-xml-names-20091208/#defaulting

Namespaces are meant to differentiate between two XML element having same name. As different XML elements can have same attribute name but different meaning. So there is no advantage of having namespace tag for an attribute as XML attributes are considered to be part of "element namespace" only.
In your example
<ett:IncidentEvent EventTypeText="Beginning" xmlns:ett="http://foo">
<ett:EventDate>2013-12-18</ett:EventDate>
<ett:EventTime>00:15:28</ett:EventTime>
</ett:IncidentEvent>
EventTypeText is part of namespace ett:IncidentEvent
Please refer to http://www.w3.org/TR/REC-xml-names/ for XML Namespaces

I'll give KKD credit for the answer, but I discovered another scenario that still causes issues. Apparently if the object to be serialized is a child of another object, if the parent object's namespace is the same as the child's, the serializer assumes you don't need to explicitly declare the namespace for the child.
public class IncidentEvent : IXmlSerializable
{
public string EventDate { get; set; }
public string EventTime { get; set; }
public string EventTypeText { get; set; }
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
return null;
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("ex", "EventTypeText", "http://foo", EventTypeText);
}
}
By implementing IXmlSerializable, I can manually write out the elements and attributes in exactly the way I need. Since this is a one-way export, I didn't need to implement anything but the WriteXml method.
I'm still not sure if this is the best way, but it works for the moment.

Related

XNamespace Prefix on XmlAttribute? [duplicate]

I need to generate the following XML during serialization:
(fragment)
<IncidentEvent a:EventTypeText="Beginning" xmlns:a="http://foo">
<EventDate>2013-12-18</EventDate>
<EventTime>00:15:28</EventTime>
</IncidentEvent>
The class in question looks like this:
public class IncidentEvent
{
public string EventDate { get; set; }
public string EventTime { get; set; }
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
It appears that the serializer is noticing that the namespace is already declared in an xmlns: at the root and is ignoring my attribute. I also tried the following:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
public string EventDate { get; set; }
public string EventTime { get; set; }
private XmlSerializerNamespaces _Xmlns;
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Xmlns
{
get
{
if (_Xmlns == null)
{
_Xmlns = new XmlSerializerNamespaces();
_Xmlns.Add("ett", "http://foo");
}
return _Xmlns;
}
set
{
_Xmlns = value;
}
}
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
This results in the following XML:
<ett:IncidentEvent EventTypeText="Beginning" xmlns:ett="http://foo">
<ett:EventDate>2013-12-18</ett:EventDate>
<ett:EventTime>00:15:28</ett:EventTime>
</ett:IncidentEvent>
Which is not what I want. The element shouldn't be prefixed, the attribute should be. What is needed to get the serializer to understand what I want?
This may be a bug in XmlSerializer.
As you have noticed, even when XmlAttributeAttribute.Namespace is set explicitly, the attribute will not be prefixed in certain situations. From testing, this appears to happen when the attribute namespace happens to be the same as the namespace of the element currently being written.
For instance:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
Serializes to the following XML:
<q1:IncidentEvent EventTypeText="an attribute" xmlns:q1="http://foo" />
And since the attribute is unprefixed, it's actually not in any namespace, as is explained in the XML standard: The namespace name for an unprefixed attribute name always has no value.
However, the following:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
[XmlAttribute("EventTypeText", Namespace = "http://bar")]
public string EventTypeText { get; set; }
}
Serializes with the attribute correctly prefixed:
<q1:IncidentEvent p1:EventTypeText="an attribute" xmlns:p1="http://bar" xmlns:q1="http://foo" />
The workaround is to explicitly set [XmlAttribute(Form = XmlSchemaForm.Qualified)]. Thus:
[XmlRoot(Namespace = "http://foo")]
public class IncidentEvent
{
[XmlAttribute("EventTypeText", Namespace = "http://foo", Form = XmlSchemaForm.Qualified)]
public string EventTypeText { get; set; }
}
Serializes to
<q1:IncidentEvent q1:EventTypeText="an attribute" xmlns:q1="http://foo" />
as required.
I did some research may be following answer helps
For Attributes to have namespace prefix you have to specify a different namespace tag other than what you have specified http://foo. Following code hopefully will solve your issue. In the code i have remove the namespace for elements and added only for the attribute.
public class IncidentEvent
{
public string EventDate { get; set; }
public string EventTime { get; set; }
[XmlAttribute("EventTypeText", Namespace = "http://foo")]
public string EventTypeText { get; set; }
}
class Program
{
static void Main(string[] args)
{
IncidentEvent xmlObj = new IncidentEvent()
{
EventDate = "2012.12.01",
EventTime = "1:00:00",
EventTypeText = "Beginining"
};
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("ett", "http://foo");
XmlSerializer serializer = new XmlSerializer(typeof(IncidentEvent));
serializer.Serialize(Console.OpenStandardOutput(), xmlObj, ns);
Console.WriteLine();
}
}
http://www.w3.org/TR/2009/REC-xml-names-20091208/#defaulting
Namespaces are meant to differentiate between two XML element having same name. As different XML elements can have same attribute name but different meaning. So there is no advantage of having namespace tag for an attribute as XML attributes are considered to be part of "element namespace" only.
In your example
<ett:IncidentEvent EventTypeText="Beginning" xmlns:ett="http://foo">
<ett:EventDate>2013-12-18</ett:EventDate>
<ett:EventTime>00:15:28</ett:EventTime>
</ett:IncidentEvent>
EventTypeText is part of namespace ett:IncidentEvent
Please refer to http://www.w3.org/TR/REC-xml-names/ for XML Namespaces
I'll give KKD credit for the answer, but I discovered another scenario that still causes issues. Apparently if the object to be serialized is a child of another object, if the parent object's namespace is the same as the child's, the serializer assumes you don't need to explicitly declare the namespace for the child.
public class IncidentEvent : IXmlSerializable
{
public string EventDate { get; set; }
public string EventTime { get; set; }
public string EventTypeText { get; set; }
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
return null;
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("ex", "EventTypeText", "http://foo", EventTypeText);
}
}
By implementing IXmlSerializable, I can manually write out the elements and attributes in exactly the way I need. Since this is a one-way export, I didn't need to implement anything but the WriteXml method.
I'm still not sure if this is the best way, but it works for the moment.

XML Serialize for inherited objects C#

I have the following XML Scenario:
<al2:Dispatch xmlns:al1="al:1.0.0" xmlns:al2="al:2.0.0">
<al2:MsgIdentificator>0001</al2:MsgIdentificator>
..
<al1:DispatchReceiverGroup>
<al2:Receiver>
<al1:ANumberIdentificator>100001</al1:ANumberIdentificator>
<al1:BNumberIdentificator>1000000001</al1:BNumberIdentificator>
</al2:Receiver>
</al1:DispatchReceiverGroup>
</al2:Dispatch>
So My Model is as follows:
[Serializable]
[XmlRoot(Namespace = "al:2.0.0", ElementName = "Dispatch")]
public class BatchDistribution
{
[XmlElement(Namespace = "al:2.0.0", ElementName = "MsgIdentificator")]
public string MessageIdentificator { get; set; }
//CONFUSED HERE
[XmlArray(Namespace = "al:1.0.0", ElementName = "DispatchReceiverGroup")]
[XmlArrayItem(Namespace = "al:2.0.0", ElementName = "Receiver")]
public List<DistributionReceiver> DistributionRecievers { get; set; }
}
}
So I want to have an optional amount of Elements of ANumberIdentificator and BNumberIdentificator elements. For this, I made a Base Class DistributionReceiver, which is inherited by DistributionReceiverA and DistributionReceiverB, such as follows:
[Serializable]
public class DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0")] //note i dont assign value for Element name because it has to be decided by sub classes
public string NumberIdentificator{ get; set; }
}
and the sub classes:
[Serializable]
public class DistributionRecieverA : DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0", ElementName = "ANumberIdentificator")]
public new string ANumberIdentificator { get; set; }
}
and the other
[Serializable]
public class DistributionRecieverB : DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0", ElementName = "BNumberIdentificator")]
public new string BNumberIdentificator { get; set; }
}
Problem is: I don't get serialized ANumberIdentificator and BNumberIdentificator.
Looks like the model is incorrect for ANumberIdentificator and BNumberIdentificator
I have used xmltocsharp to convert the example XML and the model looked like
[XmlRoot(ElementName = "Receiver", Namespace = "al:2.0.0")]
public class Receiver
{
[XmlElement(ElementName = "ANumberIdentificator", Namespace = "al:1.0.0")]
public List<string> ANumberIdentificator { get; set; }
[XmlElement(ElementName = "BNumberIdentificator", Namespace = "al:1.0.0")]
public List<string> BNumberIdentificator { get; set; }
}
[XmlRoot(ElementName = "DispatchReceiverGroup", Namespace = "al:1.0.0")]
public class DispatchReceiverGroup
{
[XmlElement(ElementName = "Receiver", Namespace = "al:2.0.0")]
public Receiver Receiver { get; set; }
}
[XmlRoot(ElementName = "Dispatch", Namespace = "al:2.0.0")]
public class Dispatch
{
[XmlElement(ElementName = "MsgIdentificator", Namespace = "al:2.0.0")]
public string MsgIdentificator { get; set; }
[XmlElement(ElementName = "DispatchReceiverGroup", Namespace = "al:1.0.0")]
public DispatchReceiverGroup DispatchReceiverGroup { get; set; }
[XmlAttribute(AttributeName = "al1", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Al1 { get; set; }
[XmlAttribute(AttributeName = "al2", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Al2 { get; set; }
}
I have used XmlSerializer to convert the XML to object
XmlSerializer serializer = new XmlSerializer(typeof(Dispatch));
using (TextReader reader = new StringReader(xmlstring))
{
//convert the xml to object
Dispatch result = (Dispatch)serializer.Deserialize(reader);
}
And I am able to read A and B under Receiver
Console.WriteLine(result.DispatchReceiverGroup.Receiver.ANumberIdentificator);
Console.WriteLine(result.DispatchReceiverGroup.Receiver.BNumberIdentificator);
The XmlArrayItem must enumerate all possible types in your array
XmlArrayItem(ElementName="DistributionReceiver", Type=typeof(DistributionReciever)),
XmlArrayItem(ElementName="DistributionRecieverA", Type=typeof(DistributionRecieverA)),
XmlArrayItem(ElementName="DistributionRecieverB", Type=typeof(DistributionRecieverB)),
The serializer need to conclude, from ElementName to Runtime-Type. (add your namespace yourself). The place of XmlArrayItem is correct, but add multiple !
If your base-class is abstract, do not create an XmlArrayItem for it.

Deserialize a xml file with child elements in c#

I am trying to Deserialize a xml file of this type
<?xml version="1.0" encoding="UTF-8"?>
<Network>
<ROUTES>
<ROUTE ID="RT_BALA_GLNC_R_162_154_1" DIRECTION="LEFT" ZONE="Richmond_Hill">
<ENTRANCESIGNAL>BALA_GLNC_G162</ENTRANCESIGNAL>
<EXITSIGNAL>BALA_DONS_G154</EXITSIGNAL>
<POINTENDIDS>
<POINTENDID POS="N">PT_BALA_GLNC_W11.TrackPortionConnection</POINTENDID>
<POINTENDID POS="N">PT_BALA_GLNC_W23.TrackPortionConnection</POINTENDID>
</POINTENDIDS>
</ROUTE>
<ROUTE ID="RT_BALA_ORLS_R_111_119_1" DIRECTION="RIGHT" ZONE="Richmond_Hill">
<ENTRANCESIGNAL>BALA_ORLS_G111</ENTRANCESIGNAL>
<EXITSIGNAL>BALA_ORLN_G119</EXITSIGNAL>
<POINTENDIDS>
<POINTENDID POS="N">PT_BALA_ORLS_W1.TrackPortionConnection</POINTENDID>
</POINTENDIDS>
</ROUTE>
<ROUTE ID="RT_BALA_GLNC_R_162D_154_1" DIRECTION="LEFT" ZONE="Richmond_Hill">
<ENTRANCESIGNAL>BALA_GLNC_G162D</ENTRANCESIGNAL>
<EXITSIGNAL>BALA_DONS_G154</EXITSIGNAL>
<POINTENDIDS>
<POINTENDID POS="R">PT_BALA_GLNC_W11.TrackPortionConnection</POINTENDID>
<POINTENDID POS="N">PT_BALA_GLNC_W23.TrackPortionConnection</POINTENDID>
</POINTENDIDS>
</ROUTE>
</ROUTES>
</Network>
I have tried this
class Program
{
static void Main(string[] args)
{
XmlSerializer deserializer = new XmlSerializer(typeof(Network));
TextReader reader = new StreamReader(#"xml File Location");
object obj = deserializer.Deserialize(reader);
Network XmlData = (Network)obj;
reader.Close();
Console.ReadLine();
}
}
[XmlRoot("Network")]
public class Network
{
[XmlElement("ROUTES")]
public List<ROUTE> ROUTES { get; set; }
}
public class ROUTE
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlAttribute("DIRECTION")]
public string DIRECTION { get; set; }
[XmlElement("ENTRANCESIGNAL")]
public string ENTRANCESIGNAL { get; set; }
[XmlElement("EXITSIGNAL")]
public string EXITSIGNAL { get; set; }
[XmlElement("POINTENDIDS")]
public POINTENDIDS POINTENDIDS { get; set; }
}
public class POINTENDIDS
{
[XmlElement("POINTENDID")]
public List<POINTENDID> POINTENDID { get; set; }
}
public class POINTENDID
{
[XmlAttribute("POS")]
public string POS { get; set; }
}
I am doing it in a console application,
I started Debugging and put breakpoint on Network XmlData = (Network)obj;
I've got only 1 ROUTES and the values of "ID", "DIRECTION", "ENTRANCESIGNAL" ...etc are set to Null
being beginner in c# programming , I don't really understand what should I do !
Need help for this implementation
Fix you Network Class. The names in square brackets are case sensitive. You also need to add the Xml array attributes.
[XmlRoot("Network")]
public class Network
{
[XmlArrayItem("ROUTE")]
[XmlArray("ROUTES")]
public List<ROUTE> ROUTES { get; set; }
}
using System.Xml; //XmlDoc
using System.Xml.Linq;//XElement
using System.IO;//Path,File,Directory, Stream
Read and parse xml file:
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(XmlFilePath);
Another approach is using XElement instead:
XElement a = XElement.Load(#"c:\path\file");
Most often I prefer XElement over XmlDocument, but that's personal
If you are starting with C#, you'd need a book, and a simpler project. Streams and Xml are syntactically complex. Also, console apps are ugly, and Forms apps are not that hard to do with the graphical tools of VisualStudio.
Your C# classes are not exactly aligned with the XML file and the serializer returns only a partial result. What you can do instead if the XML structure is fixed is outlined here.
https://stackoverflow.com/a/17315863/99804
This then works as you want it to.
You will get the following auto-generated code.
Note: I have cleaned up the output to use auto-properties etc.
using System;
using System.Xml.Serialization;
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks />
[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Network
{
/// <remarks />
[XmlArrayItem("ROUTE", IsNullable = false)]
public NetworkROUTE[] ROUTES { get; set; }
}
[Serializable]
[XmlType(AnonymousType = true)]
public class NetworkROUTE
{
[XmlAttribute]
public string DIRECTION { get; set; }
public string ENTRANCESIGNAL { get; set; }
public string EXITSIGNAL { get; set; }
[XmlAttribute]
public string ID { get; set; }
[XmlArrayItem("POINTENDID", IsNullable = false)]
public NetworkROUTEPOINTENDID[] POINTENDIDS { get; set; }
[XmlAttribute]
public string ZONE { get; set; }
}
[Serializable]
[XmlType(AnonymousType = true)]
public class NetworkROUTEPOINTENDID
{
[XmlAttribute]
public string POS { get; set; }
[XmlText]
public string Value { get; set; }
}

Deserialize XML into C# object with list

I'm trying to deserialize an XML into a C# object that has numerous elements of the same type. I've pared down the contents for clarity. My C# class looks like this:
[XmlInclude(typeof(RootElement))]
[XmlInclude(typeof(Entry))]
[Serializable, XmlRoot("Form")]
public class DeserializedClass
{
public List<Entry> listEntry;
public RootElement rootElement { get; set; }
}
Then I define the Entry and RootElement classes as follows:
public class RootElement
{
public string rootElementValue1 { get; set; }
public string rootElementValue2 { get; set; }
}
public class Entry
{
public string entryValue1 { get; set; }
public string entryValue2 { get; set; }
}
And the structure of the XML I'm trying to deserialize looks like this:
<Entry property="value">
<entryValue1>Data 1</entryValue1>
<entryValue2>Data 2</entryValue2>
<RootElement>
<rootElementValue1>Data 3</rootElementValue1>
<rootElementValue2>Data 4</rootElementValue2>
</RootElement>
<RootElement>
<rootElementValue1>Data 5</rootElementValue1>
<rootElementValue2>Data 6</rootElementValue2>
</RootElement>
</Entry>
As you can see there will be multiple RootElement elements that I want to deserialize into the List of the C# object. To deserialize I use the following:
XmlSerializer serializer = new XmlSerializer(typeof(DeserializedClass));
using (StringReader reader = new StringReader(xml))
{
DeserializedClass deserialized = (DeserializedClass)serializer.Deserialize(reader);
return deserialized;
}
Any ideas how to fix it?
I tweaked your classes a little bit for your deserialization code to work:
[Serializable, XmlRoot("Entry")]
public class DeserializedClass
{
public string entryValue1;
public string entryValue2;
[XmlElement("RootElement")]
public List<RootElement> rootElement { get; set; }
}
public class RootElement
{
public string rootElementValue1 { get; set; }
public string rootElementValue2 { get; set; }
}
Now it works fine.
I don't know why you declared your XmlRoot as "Form" as there is no element in the XML with that name so I replaced it with "Entry".
You cannot use an Entry class with entryvalue1 and entryvalue2 properties because they are direct children of the root (Event) and there is no child as Entry. In short your classes must reflect the hierarchy of the XML so that deserialization can work properly.

Deserialize XML Elements Return null 3

I have looked at several other questions for this same issue but have not been able to resolve my problem. I have classes, code, and XML as follows. However, after the Deserialize call my type variable contains a TestList array which contains a TestElement but the TestElement is null. Appreciate any help. Thanks.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace TestXMLSerialization
{
class Program
{
static void Main(string[] args) {
XmlSerializer ser = new XmlSerializer(typeof(TestRootElement));
string xmlString = "<?xml version=\"1.0\" ?><TestRootElement><TestList><TestItem><TestElement>Test Data</TestElement></TestItem></TestList></TestRootElement>";
TestRootElement type = (TestRootElement)ser.Deserialize(new StringReader(xmlString));
Console.WriteLine(type.TestList[0].TestElement);
}
}
[Serializable()]
[System.Xml.Serialization.XmlRoot("TestRootElement")]
public class TestRootElement
{
[System.Xml.Serialization.XmlElement("TestList")]
public List<TestItem> TestList { get; set; }
}
[Serializable()]
[System.Xml.Serialization.XmlType("TestItem")]
public class TestItem
{
[System.Xml.Serialization.XmlElement("TestElement")]
public string TestElement { get; set; }
}
}
Turns out the code works fine without any Serialization attributes.
public class TestRootElement
{
public List<TestItem> TestList { get; set; }
}
public class TestItem
{
public string TestElement { get; set; }
}
So adding the attributes back in one at a time I found that the List<> needed an XmlArray attribute instead of XmlElement.
[Serializable()]
[System.Xml.Serialization.XmlRoot("TestRootElement")]
public class TestRootElement
{
[XmlArray("TestList")]
public List<TestItem> TestList { get; set; }
}
[Serializable()]
[XmlType("TestItem")]
public class TestItem
{
[XmlElement("TestElement")]
public string TestElement { get; set; }
}

Categories