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.