locked
Serializing a Point Using Xml Attributes RRS feed

  • Question

  • I have a class with a property declared as follows:
    Public Property StartPoint() As Point

    As you can see, this is a very simple property, everything is working fine. However, when this property (and any other property of type Point) is serialized to XML, I get the following:
    <StartPoint>
      <X>75</X>
      <Y>75</Y>
    </StartPoint>

    Which works, and is what I expect, but I would like to be able to get something more like the following:
    <StartPoint X="75" Y="75"/>

    So that the generated Xml can be more compact (especially since I have a lot of properties of type Point). But because Point is not a class that I created, I cannot simply add the XmlAttribute attribute to the X and Y properties like I would for one of my classes. Is there any easy way to have the X and Y properties serialized as attributes rather than elements, or am I stuck with the X & Y elements? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Saturday, August 10, 2013 9:29 PM

Answers

  • Hi,Nathan

    I am sorry to misunderstand your question!I think you can code like this:

    Dim startpoint As New Point(75, 75)
    Dim xmlElmContact As New XElement("startpoint")
    xmlElmContact.SetAttributeValue("x", startpoint.X)
    xmlElmContact.SetAttributeValue("y", startpoint.Y)

    Best Wishes!

    • Edited by Anne Jing Wednesday, August 14, 2013 4:42 AM from
    • Marked as answer by Nathan Sokalski Wednesday, August 14, 2013 9:18 PM
    Wednesday, August 14, 2013 4:40 AM

All replies

  • Hi,Nathan

    Let me try to answer your question,you can code class object like this:

    Public Class StartPoin <XmlAttribute> _ Public Property X() As String Get Return m_X End Get Set m_X = Value End Set End Property Private m_X As String <XmlAttribute> _ Public Property Y() As String Get Return m_Y End Get Set m_Y = Value End Set End Property Private m_Y As String End Class

    Public Class SomeModel <XmlElement("StartPoin")> _ Public Property SomeString() As StartPoin Get Return m_SomeString End Get Set m_SomeString = Value End Set End Property Private m_SomeString As StartPoin End Class

    Then you can use the method named transform to serialize the property to xml attribute,code like this:

    Public Sub transform()
    	Dim model = New SomeModel() With { _
    		Key .SomeString = New StartPoin() With { _
    			Key .X = "75", _
    			Key .Y = "75" _
    		} _
    	}
    	Dim serializer = New XmlSerializer(model.[GetType]())
    	Try
    
    		Dim applicationData = Windows.Storage.ApplicationData.Current
    		Dim storageFolder = applicationData.LocalFolder
    		Dim file = Await storageFolder.CreateFileAsync("FileName.xml", CreationCollisionOption.ReplaceExisting)
    		Dim dataContractSerializer = New XmlSerializer(model.[GetType]())
    		Dim memoryStream = New MemoryStream()
    		dataContractSerializer.Serialize(memoryStream, model)
    		memoryStream.Seek(0, SeekOrigin.Begin)
    		Dim serialized As String = New StreamReader(memoryStream).ReadToEnd()
    		memoryStream.Dispose()
    		Dim doc = New XmlDocument()
    		doc.LoadXml(serialized)
    
    		Await doc.SaveToFileAsync(file)
    
    	Catch exception As Exception
    
    
    	End Try
    End Sub

    Best Wishes!


    • Edited by Anne Jing Monday, August 12, 2013 3:20 PM from
    Monday, August 12, 2013 3:20 PM
  • I think you misunderstood what I meant when I said "Point". When I said "Point", I was referring to the Windows.Foundation.Point structure. If I need to create any extra classes of my own, or copy the properties to somewhere else, or in any way modify how the properties are accessed, it is not worth it, this is just a preference as to what the Xml looks like. If this were a larger, more customized class for use specifically in my application, the case would be different, but Point is a structure built into .NET and is used by many other classes as well.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Monday, August 12, 2013 7:30 PM
  • Hi,Nathan

    I am sorry to misunderstand your question!I think you can code like this:

    Dim startpoint As New Point(75, 75)
    Dim xmlElmContact As New XElement("startpoint")
    xmlElmContact.SetAttributeValue("x", startpoint.X)
    xmlElmContact.SetAttributeValue("y", startpoint.Y)

    Best Wishes!

    • Edited by Anne Jing Wednesday, August 14, 2013 4:42 AM from
    • Marked as answer by Nathan Sokalski Wednesday, August 14, 2013 9:18 PM
    Wednesday, August 14, 2013 4:40 AM
  • That looks like it might work for manual serialization, but it looks more like you are creating an XElement and using the SetAttributeValue method. That means explicitly giving the element and attributes names, as well doing it separately everytime I serialize. I do not want to create any extra variables (like you did with xmlElmContact), I just want to be able to say "serialize this property's properties as attributes" or "serialize these properties as attributes". But the important thing is that I be able to do it in the class's declaration, not the serialization code, which is why I would have liked to be able to use an attribute like <XmlAttribute()> (except that goes deeper to apply it to the X & Y properties instead of Point). But I think your answer gives the best way to manage to get the Xml that I asked for, so I guess now that I have that I can't really ask for anything else. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, August 14, 2013 9:18 PM