473,795 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialization. Customize output.

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().ToUp per().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.Seri alization.XmlTy peAttribute(Nam espace="XML.Fun ")]
[System.Xml.Seri alization.XmlRo otAttribute(Nam espace="XML.Fun ", IsNullable=fals e)]
public class DataObject
{
//[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public GuidId gId;

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

public class GuidId : IXmlSerializabl e
{
public Guid TheGuidId;

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

public void ReadXml ( System.Xml.XmlR eader reader )
{
}

public void WriteXml ( System.Xml.XmlW riter writer )
{
writer.WriteStr ing( TheGuidId.ToStr ing().ToUpper() .Replace("-","") );
}
}
The XML output
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" >
<gId>A5E13ADD55 B7429D8E84816D5 B6FCDF5</gId>
</DataObject>
The XML output that i would like to have is
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" gId="A5E13ADD55 B7429D8E84816D5 B6FCDF5">

</DataObject>

Thank you for any help.

Nov 12 '05 #1
3 2364
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/de...ClassTopic.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

"Mark" <po******@news. group> wrote in message news:#e******** ******@TK2MSFTN GP11.phx.gbl...
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().ToUp per().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.Seri alization.XmlTy peAttribute(Nam espace="XML.Fun ")]
[System.Xml.Seri alization.XmlRo otAttribute(Nam espace="XML.Fun ", IsNullable=fals e)]
public class DataObject
{
//[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public GuidId gId;

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

public class GuidId : IXmlSerializabl e
{
public Guid TheGuidId;

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

public void ReadXml ( System.Xml.XmlR eader reader )
{
}

public void WriteXml ( System.Xml.XmlW riter writer )
{
writer.WriteStr ing( TheGuidId.ToStr ing().ToUpper() .Replace("-","") );
}
}
The XML output
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" >
<gId>A5E13ADD55 B7429D8E84816D5 B6FCDF5</gId>
</DataObject>
The XML output that i would like to have is
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" gId="A5E13ADD55 B7429D8E84816D5 B6FCDF5">

</DataObject>

Thank you for any help.

Nov 12 '05 #2
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.Seri alization.XmlAt tributeAttribut e()]. 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.Invalid OperationExcept ion' 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.

"Kapil Sachdeva" <ks************ @yahoo.com> wrote in message news:eo******** ******@TK2MSFTN GP09.phx.gbl...
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/de...ClassTopic.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

"Mark" <po******@news. group> wrote in message news:#e******** ******@TK2MSFTN GP11.phx.gbl...
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().ToUp per().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.Seri alization.XmlTy peAttribute(Nam espace="XML.Fun ")]
[System.Xml.Seri alization.XmlRo otAttribute(Nam espace="XML.Fun ", IsNullable=fals e)]
public class DataObject
{
//[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public GuidId gId;

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

public class GuidId : IXmlSerializabl e
{
public Guid TheGuidId;

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

public void ReadXml ( System.Xml.XmlR eader reader )
{
}

public void WriteXml ( System.Xml.XmlW riter writer )
{
writer.WriteStr ing( TheGuidId.ToStr ing().ToUpper() .Replace("-","") );
}
}
The XML output
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" >
<gId>A5E13ADD55 B7429D8E84816D5 B6FCDF5</gId>
</DataObject>
The XML output that i would like to have is
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" gId="A5E13ADD55 B7429D8E84816D5 B6FCDF5">

</DataObject>

Thank you for any help.

Nov 12 '05 #3
When you implement IXmlSerializabl e, 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 IXmlSerializabl e. This may work for you:

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

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor
"Mark" <po******@news. group> wrote in message news:eS******** ******@TK2MSFTN GP09.phx.gbl...
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.Seri alization.XmlAt tributeAttribut e()]. 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.Invalid OperationExcept ion' 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.

"Kapil Sachdeva" <ks************ @yahoo.com> wrote in message news:eo******** ******@TK2MSFTN GP09.phx.gbl...
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/de...ClassTopic.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

"Mark" <po******@news. group> wrote in message news:#e******** ******@TK2MSFTN GP11.phx.gbl...
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().ToUp per().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.Seri alization.XmlTy peAttribute(Nam espace="XML.Fun ")]
[System.Xml.Seri alization.XmlRo otAttribute(Nam espace="XML.Fun ", IsNullable=fals e)]
public class DataObject
{
//[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public GuidId gId;

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

public class GuidId : IXmlSerializabl e
{
public Guid TheGuidId;

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

public void ReadXml ( System.Xml.XmlR eader reader )
{
}

public void WriteXml ( System.Xml.XmlW riter writer )
{
writer.WriteStr ing( TheGuidId.ToStr ing().ToUpper() .Replace("-","") );
}
}
The XML output
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" >
<gId>A5E13ADD55 B7429D8E84816D5 B6FCDF5</gId>
</DataObject>
The XML output that i would like to have is
<?xml version="1.0" encoding="utf-16"?>
<DataObject xmlns="XML.Fun" gId="A5E13ADD55 B7429D8E84816D5 B6FCDF5">

</DataObject>

Thank you for any help.
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1022
by: Daniel Cazzulino | last post by:
While trying to customize the generated code for an XmlSchema, using the technique explained in http://weblogs.asp.net/cazzu/posts/33302.aspx, I came to a point where I can't advance any further as the framework classes are throwing an exception where they shouldn't. The customization process basically involves using the XmlSchemaImporter class and using its ImportTypeMapping() method. To further customize it, I wanted to modify the type...
0
1743
by: Todd | last post by:
What attributes/code do I need to add to a class to get GetObjectData to fire? I need to use XML serialization to serialize an object with private members including complex data structures which eventually may contain other private objects. I can get that method to fire using SOAP serialization, but shouldn't I be able to customize my serialization process using XML serialization? Thanks, Todd.
1
4255
by: Mountain Bikn' Guy | last post by:
We have an app that computes a lot of numeric data. We would like to save to disk about 1-2 gigabytes of computed data that includes ints, doubles, strings and some complex objects that contain hashtables. We would like to read this data back into the app and reuse those values and state to compute more new data. Up to this point we just write out comma separated ASCII values for everything. And we read/parse the ascii back in. The ASCII...
0
2505
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem serializing the object but there seems to be a problem serializing an array of objects. Any help will be appreciated "Cannot assign object of type System.Object to an object of type ElectronicWallet.C2PTest.PaymentItem." :...
0
1534
by: Guogang | last post by:
How can I customize the XML serialization for web service parameters? Speicifically, I am using DateTime as parameter of web service function call. I'd like to be able to control the XML serialization of the parameter, such that the client side will get a slightly different string than the defaul serialized value. Defaul serialized DateTime: <TimeStampedTime> 2004-09-22T19:42:22.0000000-06:00
5
6030
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the assembly 3- I am trying to deserialize from an EXE that references version 2.0 of the assembly 4- Both version 1.0 and 2.0 of the assembly reside in the GAC (no policy redirects exist).
2
5231
by: Abra | last post by:
Hello, I am using the XmlSerializer clas to serialize/deserialize XML files. I have a XML file containing elements which have attributes of type float. If the value of the float attribute in my application is for example 43.5678, after the serialization I get the value "43.5677999999999". Is it any way to control the formatting of the float type, to limit for example the number of positions after the comma ? The only solution that I found...
0
1155
by: Joe Fawcett | last post by:
<sfa.roy@gmail.comwrote in message news:7243a1eb-3fa3-43e0-8339-d90f890e6716@b1g2000hsg.googlegroups.com... Well you can change some of the behaviour using attributes on the field but only to a limited extent. What do you want to do? -- Joe Fawcett (MVP - XML) http://joe.fawcett.name
12
3080
by: =?Utf-8?B?enRSb24=?= | last post by:
Hi all, I recently came across something really strange and after a couple of days of debugging, I finally nailed the cause of it. However, I have absolutely no idea what I am doing wrong or is it just a bug in binary serialization. The following is a simple example of the code:
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.