473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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().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(Namespac e="XML.Fun")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="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.

Nov 12 '05 #1
3 2338
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**************@TK2MSFTNGP11.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().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(Namespac e="XML.Fun")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="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.

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.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.

"Kapil Sachdeva" <ks************@yahoo.com> wrote in message news:eo**************@TK2MSFTNGP09.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**************@TK2MSFTNGP11.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().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(Namespac e="XML.Fun")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="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.

Nov 12 '05 #3
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
"Mark" <po******@news.group> wrote in message news:eS**************@TK2MSFTNGP09.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.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.

"Kapil Sachdeva" <ks************@yahoo.com> wrote in message news:eo**************@TK2MSFTNGP09.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**************@TK2MSFTNGP11.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().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(Namespac e="XML.Fun")]
[System.Xml.Serialization.XmlRootAttribute(Namespac e="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.
Nov 12 '05 #4

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

Similar topics

0
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...
0
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...
1
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...
0
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...
0
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...
5
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...
2
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...
0
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...
12
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.