Re: XmlSerializer
From: Loui Mercieca (loui_at_gfi.com)
Date: 11/18/04
- Next message: Carlo Garcia: "Re: Headache with namespaces ..."
- Previous message: Geoff: "Deserialize from SelectSingleNode"
- In reply to: Dino Chiesa [Microsoft]: "Re: XmlSerializer"
- Next in thread: Dino Chiesa [Microsoft]: "Re: XmlSerializer"
- Reply: Dino Chiesa [Microsoft]: "Re: XmlSerializer"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 18 Nov 2004 09:13:59 +0100
Hi,
I did manage to do this by using the following code:
//Create a new empty name spave, used to ovveride the default
XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces();
emptyNamespace.Add("", "");
//Declare a new XmlSerializer instance, with the type of an array of
FormField, and the default attribute.
XmlSerializer serializer = new XmlSerializer(typeof(FormField[]));
//Declare a new StringWriter used as the place to store the serialized xml
StringWriter writer = new StringWriter();
//Serialize the array, using the empty namespace
serializer.Serialize( writer, this.Fields,emptyNamespace );
However now i face a new problem. Although i am declaring and
XmlRootAttribute i am still getting the class name as the the root. What am
i doing wrong?
The declaration is as follows
[XmlRootAttribute("field")]
public class FormField
The result i get is as follows
<fieldsRoot>
<FormField name = [name]>[value]</FormField>
</fieldsRoot>
"Dino Chiesa [Microsoft]" <dinoch@online.microsoft.com> wrote in message
news:eTnQgPMzEHA.2656@TK2MSFTNGP14.phx.gbl...
>> Whilst the result i want is:
>>
>> <?xml version= "1.0" encoding= "utf-16"?>
>> <fieldsRoot>
>> <field name = [name]>[value]</field>
>> </fieldsRoot>
>>
>
> Loui, you don't show your code for the collection class; I suspect that is
> the problem. Attached below is some code that does what you want.
>
>> Furthermore is there a way, to ommit the default namespaces?
>
> Yes, the way to do this is explicitly specify a collection of namespaces
> to include in the root element, and in that collection, add a blank
> namespace.
> so,
>
> XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
> ns.Add( "", "" );
> XmlSerializer s1 = new XmlSerializer(typeof(FormFieldCollection));
> FormFieldCollection fc= new FormFieldCollection();
> // add elements here ....
> s1.Serialize(System.Console.Out, fc, ns);
>
> This only works if xsi and xsd are *not* used in your instance. If
> either one is required, it will appear in the serialized stream.
>
>
> -D
>
> --
> Dino Chiesa
> Microsoft Developer Division
> d i n o c h @ OmitThis . m i c r o s o f t . c o m
>
>
>
> ---- begin code ----
> using System.IO;
> using System.Xml.Serialization;
>
> [XmlRoot(ElementName="field", Namespace=null)]
> public class FormField {
>
> public FormField() {}
> public FormField(string Name, string Value) {
> name= Name;
> value= Value;
> }
> [XmlAttribute]
> public string name;
>
> [XmlText]
> public string value;
> }
>
>
> // here is the collection class
> // Note: cannot use attributes on a type derived from CollectionBase !
> //
> // [XmlRoot("fieldsRoot", Namespace="", IsNullable=false)]
> // [XmlType("fieldsRoot", Namespace="")]
> public class FormFieldCollection : System.Collections.CollectionBase {
> public FormFieldCollection() {}
>
> public int Add(FormField field)
> {
> return List.Add(field);
> }
>
> public FormField this[int index]
> {
> get { return(( FormField)List[index]); }
> set { List[index] = value; }
> }
> }
>
>
>
> namespace Ionic {
>
> // useful for suppressing the XML Declaration line
> public class XmlTextWriterFormattedNoDeclaration :
> System.Xml.XmlTextWriter {
> public XmlTextWriterFormattedNoDeclaration (System.IO.TextWriter w) :
> base(w) { Formatting= System.Xml.Formatting.Indented;}
> public override void WriteStartDocument () { }
> }
>
>
> public class TestDriver {
>
> static void Main(string[] args) {
>
> try {
>
> FormFieldCollection fc= new FormFieldCollection();
>
> XmlSerializer s1 = new XmlSerializer(typeof(FormFieldCollection));
>
> // explicitly specify the namespace collection to suppress default
> namespace entries in the root elt:
> XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
> ns.Add( "", "" );
>
>
> fc.Add(new FormField("one", "Remember the human"));
> fc.Add(new FormField("two", "Adhere to the same standards of
> behavior online that you follow in real life"));
> fc.Add(new FormField("three", "Know where you are in cyberspace"));
>
> // use a custom TextWriter to suppress the XML declaration
>
> System.Console.WriteLine("\n============================================\nSerialized:");
> s1.Serialize(new
> XmlTextWriterFormattedNoDeclaration(System.Console.Out), fc, ns);
> System.Console.WriteLine("\n");
>
> // apply a root override (not possible in code attributes on a
> CollectionBase)
>
> System.Console.WriteLine("\n============================================\nSerialized
> using a root override:");
> XmlRootAttribute xRoot1 = new XmlRootAttribute();
> xRoot1.Namespace = ""; // "urn:www.example.org";
> xRoot1.ElementName = "fieldsRoot";
>
> XmlSerializer s2 = new XmlSerializer(typeof(FormFieldCollection),
> xRoot1);
>
> s2.Serialize(new
> XmlTextWriterFormattedNoDeclaration(System.Console.Out), fc, ns);
> System.Console.WriteLine("\n");
>
> }
> catch (System.Exception e1) {
> System.Console.WriteLine("Exception!\n" + e1);
> }
> }
> }
> }
>
> ---- end code ----
>
> "Loui Mercieca" <loui@gfi.com> wrote in message
> news:%23t$7PpIzEHA.576@TK2MSFTNGP14.phx.gbl...
>> Hi,
>>
>> I have created a class, named FormField , which basically contains two
>> fields, name and value. I have set the [XmlRoot(ElementName="field",
>> Namespace=null)] tag before the class and the field is set as an
>> XmlAttribute whil the name as XmlText.
>>
>> In my main class, i have created an arraylist which contains a collection
>> of this class FormField. Basically its:
>>
>> public void Add( string sName, string sValue )
>> {
>> FormField ff = new FormField( sName, sValue );
>> m_alFields.Add( ff );
>> }
>>
>> The problem arise when i try to serialise this collection. For some
>> reason, none of the fields are present, only the root element is. The
>> code i use is:
>>
>> XmlSerializer serializer = new XmlSerializer(typeof(FormFieldCollection),
>> new System.Type[] { typeof( FormField ) } );
>> StringWriter writer = new StringWriter();
>> serializer.Serialize( writer, this);
>>
>> [note: FormFieldCollection is the main class]
>>
>> The result i get is:
>>
>> <?xml version= "1.0" encoding= "utf-16"?>
>> <fieldsRoot xmlns:xsd= "http://www.w3.org/2001/XMLSchema" xmlns:xsi=
>> "http://www.w3.org/2001/XMLSchema-instance" />
>>
>> Whilst the result i want is:
>>
>> <?xml version= "1.0" encoding= "utf-16"?>
>> <fieldsRoot>
>> <field name = [name]>[value]</field>
>> </fieldsRoot>
>>
>> Furthermore is there a way, to ommit the default namespaces?
>>
>> Thanks in advance.
>>
>
>
- Next message: Carlo Garcia: "Re: Headache with namespaces ..."
- Previous message: Geoff: "Deserialize from SelectSingleNode"
- In reply to: Dino Chiesa [Microsoft]: "Re: XmlSerializer"
- Next in thread: Dino Chiesa [Microsoft]: "Re: XmlSerializer"
- Reply: Dino Chiesa [Microsoft]: "Re: XmlSerializer"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|