XmlSerializer
Hello,
I'm just getting familiar with Xml. I have used XmlSerializer to serialize my Data class to XML file. Now I'm trying to append & in some cases delete some data to and from this exisiting XML file . I can append a single string using XmlDocument & XpathNavigator, but don't know how to append a stream of data.
Thanks in advance for you help.
DonnyG
[375 byte] By [
donnyG] at [2008-3-3]
Some more information on what you're trying to do would likely be helpful. If you're trying to modify the output of the XmlSerializer, it's best to integrate with it via the attribute (i.e. XmlElement(), XmlAttribute(), etc. to declare how certain portions of your object model are serialized) or implement IXmlSerializable, which gives you very fine-grained control over serialization.
With regard to modifying XML, you can work with XmlDocument, but this requires that the entire XML document exist parsed in memory. This may not be a problem, but it can be for large infosets. If you're dealing in large amounts of XML, take a look at XmlWriter. It allows you to write well-formed XML to a stream.
If you're trying to append data from a stream to an existing XmlDocument, you'll need to read the data from the stream using StreamReader (text-based data) or BinaryReader (binary data). You can then create the appropriate elements/attributes using XmlDocument (or XmlWriter if you're going that route) and write the data in.
Hope that helped.
Hello James,
Thanks for reply.
I will try to explain a bit on what I'm trying -- I have following
class A
{
Some header fields..
class B
}
class B
{
Some header fields..
class C
}
class C
{
Some header fields..
class D
}
class D
{
Main Data
}
Class A gets serialized and deserialized -- this is not a problem.
What I want to do is after the first serialization of class A, I just want to serialize class class C and append to existing XML file. So my XML file would look like following:
<ClassA>
<ClassB>
<Name>DonnyG</Name>
<ClassC>
<Class D> Data </ClassD>
<Class D> Data </ClassD> -- Appended
<Class D> Data </ClassD> -- Appended
</ClassC>
</ClassB>
</ClassA>
Also -- I want to deserialize selected data of classD based on specific tag or file.
Again Thanks for your help.
Using the XmlSerializer, you will serialize (and deserialize) the entire object graph. Just do something like this:
A a = new A(); // I'm assuming that B, C, and D
//instances get initialized in the constructor
MemoryStream memStream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(typeof(A));
serializer.Serialize(memStream, a);
memStream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(memStream, Encoding.UTF8);
Console.WriteLine(reader.ReadToEnd());
You will need to mark up your classes with:
[Serializable] (on the class) and [XmlElement("ElementName")] (on the properties/fields)
OR
IXmlSerializable
Note that IXmlSerializable gives you much finer-grained control over the serialization process including the ability to serialize non-public members. (It's a lot like CArchive from MFC, if you've worked with that.)