472,145 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Serialize XML With Attributes Instead of Elements

I have some classes that have the <Serializable()> attribute applied,
which of course by default serializes the class properties as elements.
What I would like to do is to be able to override this behavior at
runtime to serialize the properties as attributes.

I have been exploring the XMLAttributesOverrides classes and examples
which claims:

"you can create one set of serializable classes, but serialize the
objects in multiple ways. For example, instead of serializing members
of a class instance as XML elements, you can serialize them as XML
attributes, resulting in a more efficient document to transport."

I have yet to be able to figure out how this can be acomplished
however... For a simple class like the following if I call the
SerializeXML() method I get an error.

<Serializable()> _
Public Class Class3

Public Test As String = "Test"
Private Test2 As String = "Test2"
Private Test3 As String = "Test3"

<XmlIgnore()> _
Public Property Test4() As String
Get
Return Test3
End Get
Set(ByVal Value As String)
Test3 = Value
End Set
End Property

Public Sub SerializeXML(ByRef sw As TextWriter)
Dim attrs As New XmlAttributes
Dim attr As New XmlAttributeAttribute("MyTest",
GetType(String))
attrs.XmlAttribute = attr

Dim attrOverrides As New XmlAttributeOverrides
attrOverrides.Add(Me.GetType, "Test", attrs)

Dim xs As New XmlSerializer(GetType(Class3), attrOverrides)
xs.Serialize(sw, Me)
End Sub
End Class

Can someone PLEASE tell me how I can override the behavior at runtime
to serialize as attributes instead of elelements.

Nov 12 '05 #1
7 4253
Oops. It might help a great deal if you told us what the error was. Sorry,
but it's hilarious to me, I have done this mistake myself. I would think I
would learn to state what the error I got was myself after groaning so many
times when a user would tell me they forgot what the error was and what it
was about and could not even remember enough about it to reproduce the
error.
:)
<gr******@vt.edu> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have some classes that have the <Serializable()> attribute applied,
which of course by default serializes the class properties as elements.
What I would like to do is to be able to override this behavior at
runtime to serialize the properties as attributes.

I have been exploring the XMLAttributesOverrides classes and examples
which claims:

"you can create one set of serializable classes, but serialize the
objects in multiple ways. For example, instead of serializing members
of a class instance as XML elements, you can serialize them as XML
attributes, resulting in a more efficient document to transport."

I have yet to be able to figure out how this can be acomplished
however... For a simple class like the following if I call the
SerializeXML() method I get an error.

<Serializable()> _
Public Class Class3

Public Test As String = "Test"
Private Test2 As String = "Test2"
Private Test3 As String = "Test3"

<XmlIgnore()> _
Public Property Test4() As String
Get
Return Test3
End Get
Set(ByVal Value As String)
Test3 = Value
End Set
End Property

Public Sub SerializeXML(ByRef sw As TextWriter)
Dim attrs As New XmlAttributes
Dim attr As New XmlAttributeAttribute("MyTest",
GetType(String))
attrs.XmlAttribute = attr

Dim attrOverrides As New XmlAttributeOverrides
attrOverrides.Add(Me.GetType, "Test", attrs)

Dim xs As New XmlSerializer(GetType(Class3), attrOverrides)
xs.Serialize(sw, Me)
End Sub
End Class

Can someone PLEASE tell me how I can override the behavior at runtime
to serialize as attributes instead of elelements.

Nov 12 '05 #2
Garry,

The error was "Error Reflecting Type" on line Dim xs As New
XmlSerializer(GetType(Class3), attrOverrides). I didn't post the error
message originally because I thought it was irrelevant in the context.
All I want to know is if it is possible to control the serialization of
properties at runtime as either elements or attributes using
XMLAttributeOverrides and how that can be done. My example is probably
negligble as well, but I felt it necessary to throw *something* out
there to give people some idea of what I was attempting. So... I doubt
that error message is particularly helpful, but if you've got any ideas
I'd be willing to listen.

Garry Freemyer wrote:
Oops. It might help a great deal if you told us what the error was. Sorry,
but it's hilarious to me, I have done this mistake myself. I would think I
would learn to state what the error I got was myself after groaning so many
times when a user would tell me they forgot what the error was and what it
was about and could not even remember enough about it to reproduce the
error.
:)
<gr******@vt.edu> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have some classes that have the <Serializable()> attribute applied,
which of course by default serializes the class properties as elements.
What I would like to do is to be able to override this behavior at
runtime to serialize the properties as attributes.

I have been exploring the XMLAttributesOverrides classes and examples
which claims:

"you can create one set of serializable classes, but serialize the
objects in multiple ways. For example, instead of serializing members
of a class instance as XML elements, you can serialize them as XML
attributes, resulting in a more efficient document to transport."

I have yet to be able to figure out how this can be acomplished
however... For a simple class like the following if I call the
SerializeXML() method I get an error.

<Serializable()> _
Public Class Class3

Public Test As String = "Test"
Private Test2 As String = "Test2"
Private Test3 As String = "Test3"

<XmlIgnore()> _
Public Property Test4() As String
Get
Return Test3
End Get
Set(ByVal Value As String)
Test3 = Value
End Set
End Property

Public Sub SerializeXML(ByRef sw As TextWriter)
Dim attrs As New XmlAttributes
Dim attr As New XmlAttributeAttribute("MyTest",
GetType(String))
attrs.XmlAttribute = attr

Dim attrOverrides As New XmlAttributeOverrides
attrOverrides.Add(Me.GetType, "Test", attrs)

Dim xs As New XmlSerializer(GetType(Class3), attrOverrides)
xs.Serialize(sw, Me)
End Sub
End Class

Can someone PLEASE tell me how I can override the behavior at runtime
to serialize as attributes instead of elelements.


Nov 12 '05 #3
Does anyone have any idea how this:

"you can create one set of serializable classes, but serialize the
objects in multiple ways. For example, instead of serializing members
of a class instance as XML elements, you can serialize them as XML
attributes, resulting in a more efficient document to transport."

can be accomplished?

Nov 12 '05 #4
Try serializing this:

Imports System.Xml
Imports System.Xml.Serialization

Public Class Class1

Public Name As String

<XmlAttributeAttribute()> _
Public Id As Integer

End Class

<gr******@vt.edu> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Does anyone have any idea how this:

"you can create one set of serializable classes, but serialize the
objects in multiple ways. For example, instead of serializing members
of a class instance as XML elements, you can serialize them as XML
attributes, resulting in a more efficient document to transport."

can be accomplished?

Nov 12 '05 #5
Yes, I know that will work... But you missed the point. I want to
control this behavior at runtime, not design time.

Nov 12 '05 #6
DC
Yes, you can change serialization at runtime, to decide whether to serialize
data as elements or attributes. There is a thing called XML Attribute
overrides, that you can apply at runtime, when de-serializing.

---- Begin sample ----
using System.IO;
using System.Xml.Serialization;

namespace Ionic {
public class Foo {
public int id;
public System.String name;
public System.Single value;
}
/// <summary>
/// helper class for serializing - omits the XML Declaration line (just
for human readability)
/// </summary>
public class XmlTextWriterFormattedNoDeclaration :
System.Xml.XmlTextWriter {
public XmlTextWriterFormattedNoDeclaration (System.IO.TextWriter w) :
base(w) { Formatting= System.Xml.Formatting.Indented;}
public override void WriteStartDocument () { }
}
public class TestDriver {
public static void Main() {

try {
Foo x= new Foo();

x.id= 7;
x.name= "Theodore Roosevelt";
x.value= 172.33F;

XmlSerializer s1 = new XmlSerializer(typeof(Foo));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "" );
System.Console.WriteLine("----------------------\nserialized as
elements:\n");
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Xml.XmlWriter xmlWriter = new
XmlTextWriterFormattedNoDeclaration(stringWriter);
s1.Serialize(xmlWriter, x, ns);

System.Console.WriteLine(stringWriter.ToString() + "\n\n");

System.Console.WriteLine("----------------------\nserialized as
attributes:\n");

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attributeSet;
XmlAttributeAttribute a1;

// provide an Attribute attribute
//
http://msdn.microsoft.com/library/en...ClassTopic.asp
a1 = new XmlAttributeAttribute("id");

// drop that into an attribute Set
attributeSet = new XmlAttributes();
attributeSet.XmlAttribute= a1;

// add that attribute set for the first field
overrides.Add(typeof(Foo), "id", attributeSet);

// do the same thing for the name field
a1 = new XmlAttributeAttribute("name");
attributeSet = new XmlAttributes();
attributeSet.XmlAttribute= a1;
overrides.Add(typeof(Foo), "name", attributeSet);

// and finally, the same idea for the value field
a1 = new XmlAttributeAttribute("value");
attributeSet = new XmlAttributes();
attributeSet.XmlAttribute= a1;
overrides.Add(typeof(Foo), "value", attributeSet);

// create a new instance of the serializer specifying the overrides
s1 = new XmlSerializer(typeof(Foo), overrides);

// serialize as normal
stringWriter = new System.IO.StringWriter();
xmlWriter = new XmlTextWriterFormattedNoDeclaration(stringWriter);
s1.Serialize(xmlWriter, x, ns);
System.Console.WriteLine(stringWriter.ToString() + "\n\n");

}
catch (System.Exception e1) {
System.Console.WriteLine("Exception!\n" + e1);
}
}
}
}

---- End sample ----

-Dino
dinoch // Microsoft.com

<gr******@vt.edu> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Yes, I know that will work... But you missed the point. I want to
control this behavior at runtime, not design time.

Nov 12 '05 #7
Ahh fantastic! This does indeed work! Apparently the only thing I was
doing incorrectly in my code was attempting to pass a Type to the
constructor of the XmlAttributeAttribute. I'm still not sure why this
was causing the serialization to fail, but at least this works! Thanks
much for the help!

Nov 12 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Joris Gillis | last post: by
10 posts views Thread by Dan | last post: by
reply views Thread by David Lozzi | last post: by
8 posts views Thread by Andy B | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.