XML Fragment Serialization [modified]
-
I'm working on an existing system which operates on large XML files, but the areas of interest at any given point are relatively small. So i've begun moving the small regions across to an object model, which i will serialize into / out of the existing XML document in the appropriate places in fragments. My problem is, the XMLSerializer generates the XMLFile Declaration and NameSpace attributes in the root element, which makes the end result a complete XML Document, as opposed to a fragment that is part of a larger doc. Is there any way to avoid the generation / requirement of the XML Serialization attributes so that it is actualy a Fragment? Cheers Tris
-
I'm working on an existing system which operates on large XML files, but the areas of interest at any given point are relatively small. So i've begun moving the small regions across to an object model, which i will serialize into / out of the existing XML document in the appropriate places in fragments. My problem is, the XMLSerializer generates the XMLFile Declaration and NameSpace attributes in the root element, which makes the end result a complete XML Document, as opposed to a fragment that is part of a larger doc. Is there any way to avoid the generation / requirement of the XML Serialization attributes so that it is actualy a Fragment? Cheers Tris
When you write from the XmlSerializer, you can use an XmlWriter (as a TextWriter). Use the static Create method off of XmlWriter to make a writer that has XmlWriterSettings. Using the settings, you can tell it to not put the declarations.
-
When you write from the XmlSerializer, you can use an XmlWriter (as a TextWriter). Use the static Create method off of XmlWriter to make a writer that has XmlWriterSettings. Using the settings, you can tell it to not put the declarations.
Thanks Dustin, that's just the ticket. One last thing - is it possible to remove the xmlns:xsi and xmlns:xsd attributes using settings? I've had a play around, but they are staying put. Cheers Tris
-
Thanks Dustin, that's just the ticket. One last thing - is it possible to remove the xmlns:xsi and xmlns:xsd attributes using settings? I've had a play around, but they are staying put. Cheers Tris
That one's a bit tougher. Are you using namespaces in the attributes on the type?
-
Thanks Dustin, that's just the ticket. One last thing - is it possible to remove the xmlns:xsi and xmlns:xsd attributes using settings? I've had a play around, but they are staying put. Cheers Tris
I finally got to a dev environment where I could test it. There really isn't a way to wipe those attributes off of there, which I think is a very frustrating part of using XmlSerializer. Some of the tactics I've taken in the past are to implement IXmlSerializable (which isn't fun because you have to read and write from the xml stream yourself) or to load the serialized xml into an XmlDocument and remove the namespaces manually. I'm not sure if MVPXML[^] has done anything to handle this but you may want to check it out.