so look here: https://social.msdn.microsoft.com/Forums/en-US/927ac38b-9846-40c0-a1eb-5850553b520d/ixmlserializable-and-prefixes-for-xmlroot-elements?forum=asmxandxml
And it is fundamentally WRONG.
Why? You can't use XmlElementAttribute on a TYPE. It only works on properties, fields, etc. XmlRootAttribute applies to Type.
So:
[XmlRoot("TheClass", Namespace = "http://myelem.com/")] public class MyClass : IXmlSerializable { [XmlElement("Header", "http://myelem.com/")] public HeaderClass Header {get;set;} } [Serializable] public class HeaderClass : IXmlSerializable { [ public string Name {get;set;} public string Value {get;set;} } static Main() { XmlSerializerNamespaces xsnNs = new XmlSerializerNamespaces(); xsnNs.Add("me", "http://myelem.com/"); XmlSerializer ser = new XmlSerializer(typeof(myClass)); using (StringWriter sw = new StringWriter(Console.Out)) { ser.Serialize(sw, new MyClass()) {Header = new HeaderClass()}, xmlNs); } }
Now, if in my implementation of IXmlSerializable I call:
writer.LookupPrefix("http://myelem.com/")
And I get an empty string.
So even the attempt to call writer.WriteElementString(prefix, name, ns, value) doesn't export the prefix in the final xml, because I can't Lookup what the prefix is. I can look at the property, I can even get the Custom Attribute that provides the
Element Name and the Namespace, but I can't get what the prefix for that namespace is without a damned GLOBAL variable. That defeats the purpose.
That doesn't even bring in the fact that at the start, even providing the XmlSerializerNamespaces collection to the serializer does NOTHING to affect the outcome of the root element. By the time you get into the call stack for IXmlSerializable.WriteXml() the root element has already been written to the writer. WITHOUT a prefix.
If I remove the IXmlSerializable, it outputs correctly. WHY? It appears to be a typical case of Microsoft's implementation handicapping developers by doing it one way and eliminating ANY possibility of true polymorphism. So far I've had to re-write dozens of Microsoft classes from scratch because the originals just don't work the way I need. And as par for the course, they aren't written in a true OOP fashion which would allow other developers (like myself) to tweak the behavior accordingly.
It appears XmlSerialization is another scenario that I might have to rewrite EVERYTHING.
Why have:
XmlSerializer.Serialize(writer, object, XmlSerializerNamespaces)
with a:
XmlWriter.LookupPrefix(namespace)
Which doesn't work!
I gave the prefix to namespace map to the serializer, why can't i look up the prefix from within the writer!!!!!!!!
It's like Microsoft is pulling a Dark Helmet: "Fooled You!"
If i DON'T use IXmlSerializable XmlSerializer exports accurate xml but not in the format or layout i want. If I do, i lose xml standard capabilities. This gives stupidity a whole new level.
A: I MUST be able to use XmlSerializer
B: I MUST be able to use IXmlSerializable
Query:
How do I get the two to work in EVERY possible scenario to achieve perfect XML output!
My Conclusion is it can't be done, but that's why i'm posting here in the .0001% chance that there is a solution to this dilemma.
Regards,
Jaeden "Sifo Dyas" al'Raec Ruiner
"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it
correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.