Serialization. Customize output.

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hello,

What I am trying todo is use .NET serialization to take a Guid property that is an attribute in the XML document and have its output to be the same as Guid.NewGuid().ToString().ToUpper().Replace("-","")

I have a working copy of this type of class that will work generating an element with the output i have and the following is an example of that code. What i run into is making the element an attribute then it breaks and does not work. What can i do to make this work?

[System.Xml.Serialization.XmlTypeAttribute(Namespace="XML.Fun")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XML.Fun", IsNullable=false)]
public class DataObject
{
//[System.Xml.Serialization.XmlAttributeAttribute()]
public GuidId gId;

public DataObject()
{
gId = new GuidId();
gId.TheGuidId = Guid.NewGuid();
}
}

public class GuidId : IXmlSerializable
{
public Guid TheGuidId;

public System.Xml.Schema.XmlSchema GetSchema ( )
{
return null;
}

public void ReadXml ( System.Xml.XmlReader reader )
{
}

public void WriteXml ( System.Xml.XmlWriter writer )
{
writer.WriteString( TheGuidId.ToString().ToUpper().Replace("-","") );
}
}


The XML output
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun">
<gId>A5E13ADD55B7429D8E84816D5B6FCDF5</gId>
</DataObject>


The XML output that i would like to have is
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" gId="A5E13ADD55B7429D8E84816D5B6FCDF5">

</DataObject>

Thank you for any help.
 
Attributes in a XML tag contains only the simple types and some inbuilt types like Guid.
As doc below says

http://msdn.microsoft.com/library/d...ializationXmlAttributeAttributeClassTopic.asp

I understand you want to customize the format of Guid but doing so make it a "complex type" and hence not supported as attribute.

May be some one can throw more light if I am not clear.

regards
Kapil

Hello,

What I am trying todo is use .NET serialization to take a Guid property that is an attribute in the XML document and have its output to be the same as Guid.NewGuid().ToString().ToUpper().Replace("-","")

I have a working copy of this type of class that will work generating an element with the output i have and the following is an example of that code. What i run into is making the element an attribute then it breaks and does not work. What can i do to make this work?

[System.Xml.Serialization.XmlTypeAttribute(Namespace="XML.Fun")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XML.Fun", IsNullable=false)]
public class DataObject
{
//[System.Xml.Serialization.XmlAttributeAttribute()]
public GuidId gId;

public DataObject()
{
gId = new GuidId();
gId.TheGuidId = Guid.NewGuid();
}
}

public class GuidId : IXmlSerializable
{
public Guid TheGuidId;

public System.Xml.Schema.XmlSchema GetSchema ( )
{
return null;
}

public void ReadXml ( System.Xml.XmlReader reader )
{
}

public void WriteXml ( System.Xml.XmlWriter writer )
{
writer.WriteString( TheGuidId.ToString().ToUpper().Replace("-","") );
}
}


The XML output
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun">
<gId>A5E13ADD55B7429D8E84816D5B6FCDF5</gId>
</DataObject>


The XML output that i would like to have is
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" gId="A5E13ADD55B7429D8E84816D5B6FCDF5">

</DataObject>

Thank you for any help.
 
Yes, I have to be careful that my output is an XSD simple type especially for attributes. The program works for an XML element. It does not work when I make the element an attribute in .NET with [System.Xml.Serialization.XmlAttributeAttribute()]. The output of my complex type will be an XML simple type string. If I can build the constructor also the constructor will be able to take an input of type string.

What I have encountered is this error when I make the property an XML attribute.

"An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll

Additional information: There was an error reflecting 'ObjectToXMLFun.DataObject'."

I have a feeling this is a limitation of .NET and that it does not use the same interface or something to work with attributes. If there was an interface that I could use to make this work or does the .net XmlSerializer have in it a blind code logic to only look for XML simple types and the few extra ones they have added?

If I can make it work for an XML element why can't I make it work for an XML attribute?

-thank you again.

Attributes in a XML tag contains only the simple types and some inbuilt types like Guid.
As doc below says

http://msdn.microsoft.com/library/d...ializationXmlAttributeAttributeClassTopic.asp

I understand you want to customize the format of Guid but doing so make it a "complex type" and hence not supported as attribute.

May be some one can throw more light if I am not clear.

regards
Kapil

Hello,

What I am trying todo is use .NET serialization to take a Guid property that is an attribute in the XML document and have its output to be the same as Guid.NewGuid().ToString().ToUpper().Replace("-","")

I have a working copy of this type of class that will work generating an element with the output i have and the following is an example of that code. What i run into is making the element an attribute then it breaks and does not work. What can i do to make this work?

[System.Xml.Serialization.XmlTypeAttribute(Namespace="XML.Fun")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XML.Fun", IsNullable=false)]
public class DataObject
{
//[System.Xml.Serialization.XmlAttributeAttribute()]
public GuidId gId;

public DataObject()
{
gId = new GuidId();
gId.TheGuidId = Guid.NewGuid();
}
}

public class GuidId : IXmlSerializable
{
public Guid TheGuidId;

public System.Xml.Schema.XmlSchema GetSchema ( )
{
return null;
}

public void ReadXml ( System.Xml.XmlReader reader )
{
}

public void WriteXml ( System.Xml.XmlWriter writer )
{
writer.WriteString( TheGuidId.ToString().ToUpper().Replace("-","") );
}
}


The XML output
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun">
<gId>A5E13ADD55B7429D8E84816D5B6FCDF5</gId>
</DataObject>


The XML output that i would like to have is
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" gId="A5E13ADD55B7429D8E84816D5B6FCDF5">

</DataObject>

Thank you for any help.
 
When you implement IXmlSerializable, then you are completely in charge of serializating the object. However, the XmlWriter that's handed to WriteXml is already moved past the root element, i.e. you can get the


However, you can get your format without implementing IXmlSerializable. This may work for you:

public class DataObject
{
[XmlAttribute]
public string gId
{
get
{
return TheGuidId.ToString().ToUpper().Replace("-","");
}
set
{
// your turn
}
}
}

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor
Yes, I have to be careful that my output is an XSD simple type especially for attributes. The program works for an XML element. It does not work when I make the element an attribute in .NET with [System.Xml.Serialization.XmlAttributeAttribute()]. The output of my complex type will be an XML simple type string. If I can build the constructor also the constructor will be able to take an input of type string.

What I have encountered is this error when I make the property an XML attribute.

"An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll

Additional information: There was an error reflecting 'ObjectToXMLFun.DataObject'."

I have a feeling this is a limitation of .NET and that it does not use the same interface or something to work with attributes. If there was an interface that I could use to make this work or does the .net XmlSerializer have in it a blind code logic to only look for XML simple types and the few extra ones they have added?

If I can make it work for an XML element why can't I make it work for an XML attribute?

-thank you again.

Attributes in a XML tag contains only the simple types and some inbuilt types like Guid.
As doc below says

http://msdn.microsoft.com/library/d...ializationXmlAttributeAttributeClassTopic.asp

I understand you want to customize the format of Guid but doing so make it a "complex type" and hence not supported as attribute.

May be some one can throw more light if I am not clear.

regards
Kapil

Hello,

What I am trying todo is use .NET serialization to take a Guid property that is an attribute in the XML document and have its output to be the same as Guid.NewGuid().ToString().ToUpper().Replace("-","")

I have a working copy of this type of class that will work generating an element with the output i have and the following is an example of that code. What i run into is making the element an attribute then it breaks and does not work. What can i do to make this work?

[System.Xml.Serialization.XmlTypeAttribute(Namespace="XML.Fun")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XML.Fun", IsNullable=false)]
public class DataObject
{
//[System.Xml.Serialization.XmlAttributeAttribute()]
public GuidId gId;

public DataObject()
{
gId = new GuidId();
gId.TheGuidId = Guid.NewGuid();
}
}

public class GuidId : IXmlSerializable
{
public Guid TheGuidId;

public System.Xml.Schema.XmlSchema GetSchema ( )
{
return null;
}

public void ReadXml ( System.Xml.XmlReader reader )
{
}

public void WriteXml ( System.Xml.XmlWriter writer )
{
writer.WriteString( TheGuidId.ToString().ToUpper().Replace("-","") );
}
}


The XML output
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun">
<gId>A5E13ADD55B7429D8E84816D5B6FCDF5</gId>
</DataObject>


The XML output that i would like to have is
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" gId="A5E13ADD55B7429D8E84816D5B6FCDF5">

</DataObject>

Thank you for any help.
 
Back
Top