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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…