Serialization/Deserialization

D

Dave

Hi all,

I know there is a lot of information on here about Serialization, but I
can't find what I need so I thought i would ask.

I am trying to serialize and deserialize a set of data. Doesn't sound
too hard. The only problem is, I have a number of elements in my XML
File that do not refer to specific variables and I don't know until I
deserialize what those are.

ie.

<XML>
<ident1 />
<ident2 />
<ident3>....</ident3>
..
..
..
</XML>

As shown above, some of my tags are empty and in these cases I need to
retrieve that Tag name not the details stored in the element. For
ident3, i need to retrieve just the element data which I have been able
to do. As ident1 and ident2 can be anything I have no idea how to
deserialize the file!

If anyone can help I would be most glad!

Thank you
 
N

Nicholas Paldino [.NET/C# MVP]

Dave,

Are you trying to put this in a data set? If so, there should be
columns which have the name of the element, as well as data in rows which
has the row data (if there is any).

Hope this helps.
 
N

Nicholas Paldino [.NET/C# MVP]

If it is going into an object, then what are you doing trying to do it
yourself? Why not use the XML serializer, or use the binary or soap
formatters?
 
D

Dave

The following code is what I have. It serialises fine, the Dave
element is serialized as the Name of the element, not its value. On
Deserialize I need to store the Name of the Dave element in Variable C
and also the same for INV.

I am using serialization, but i can't deserialize the Dave value as it
will always change, so a variable in the class cannot always be of the
same name as the element.

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Text;


[XmlRootAttribute("CMD")]
public class INVALID
{
[XmlAttribute]
public string O;
[XmlAttribute]
public string D;
public string C;
public string INV;
}

public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeMe();
//test.DeserializeStudent("Student.xml");
}

public INVALID u;
public XmlSerializer xser;
public void SerializeMe()
{
TextWriter writer = new StreamWriter("Test.xml");
u = new INVALID();
u.D = "3048";
u.O = "PC";
u.C = "Dave";
u.INV = "";

//Override C's element to display the dynamic element name
XmlElementAttribute myElementAttribute = new XmlElementAttribute();
myElementAttribute.ElementName = u.C ;
myElementAttribute.IsNullable = false;
u.C = "";
XmlAttributes myAttributes = new XmlAttributes();
myAttributes.XmlElements.Add(myElementAttribute);
XmlAttributeOverrides myOverrides = new XmlAttributeOverrides();
myOverrides.Add(typeof(INVALID), "C", myAttributes);

xser= new XmlSerializer(typeof (INVALID), myOverrides);
/*
* Remove default namespaces
*/
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
StringBuilder sb = new StringBuilder();
XmlTextWriterFormattedNoDeclaration tr = new
XmlTextWriterFormattedNoDeclaration(new StringWriter(sb));
tr.Formatting = Formatting.None;
//serialize
xser.Serialize(tr, u, ns);
//sb stores serialized string
Console.WriteLine(sb.ToString());

//deserialize
u = (INVALID)xser.Deserialize(new StringReader(sb.ToString()));
Console.WriteLine("u.D = {1}, u.O = {2}, u.C = (name):{0}, u.INV =
(name):{3}", u.C, u.D, u.O, u.INV);
while(true)
{}
}

public class XmlTextWriterFormattedNoDeclaration :
System.Xml.XmlTextWriter
{
public XmlTextWriterFormattedNoDeclaration( System.IO.TextWriter w) :
base(w)
{
Formatting= System.Xml.Formatting.Indented;
}
public override void WriteStartDocument() { }
}
}
 
K

Kevin Spencer

What? Post a message asking if no one knows how to post a message?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 
D

Dave

Well there are 7 messages in this post now, so if you can only see one
then I suggest you get yourself some stronger glasses
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top