PC Review


Reply
Thread Tools Rate Thread

Configuring XML Serialization to not add an element

 
 
Yash Ganthe
Guest
Posts: n/a
 
      31st Mar 2010
Hi,

I have the follwoing class structure:

public class MyClass {
public Fields Fields;
}

public partial class Fields {

public BaseField[] myFields;

}

[System.Xml.Serialization.XmlInclude(typeof(DerivedField))]
public class BaseField
{
//some properties
}

[XmlRoot("derived")]
public partial class DerivedField : BaseField {
}

All the classes are tagged as serilizable.
When I XMLSerialize an object of type MyClass, here is what I get:
<MyClass>
<Fields>
<myFields>
<derived name="JobID" id="A" precision="9" />
</myFields>
</Fields>
</MyClass>

The serializer adds an XML element for every public property, which is
why myfields appears as a tag in the XML.
This is undesirable in my application as I would like the elements in
myFields[] to get serialized directly under <Fields> as :
<MyClass>
<Fields>
<derived name="JobID" id="A" precision="9" />
<derived name="JobID" id="B" precision="9" />
<derived1 name="JobID" id="C" precision="9" />
<derived2 name="JobID" id="D" precision="9" />
</Fields>
</MyClass>

Is there a way to instruct the serializer using Attributes or some
other to not add a tag for the myFields array?


Thanks,
Yash
 
Reply With Quote
 
 
 
 
Hovhannes Terteryan
Guest
Posts: n/a
 
      31st Mar 2010
Hi,

I suppose you either need to implement IXmlSerializable in MyClass, or make
the class Fields as a collection.


"Yash Ganthe" <(E-Mail Removed)> wrote in message
news:95a32c1f-df23-4221-a775-(E-Mail Removed)...
> Hi,
>
> I have the follwoing class structure:
>
> public class MyClass {
> public Fields Fields;
> }
>
> public partial class Fields {
>
> public BaseField[] myFields;
>
> }
>
> [System.Xml.Serialization.XmlInclude(typeof(DerivedField))]
> public class BaseField
> {
> //some properties
> }
>
> [XmlRoot("derived")]
> public partial class DerivedField : BaseField {
> }
>
> All the classes are tagged as serilizable.
> When I XMLSerialize an object of type MyClass, here is what I get:
> <MyClass>
> <Fields>
> <myFields>
> <derived name="JobID" id="A" precision="9" />
> </myFields>
> </Fields>
> </MyClass>
>
> The serializer adds an XML element for every public property, which is
> why myfields appears as a tag in the XML.
> This is undesirable in my application as I would like the elements in
> myFields[] to get serialized directly under <Fields> as :
> <MyClass>
> <Fields>
> <derived name="JobID" id="A" precision="9" />
> <derived name="JobID" id="B" precision="9" />
> <derived1 name="JobID" id="C" precision="9" />
> <derived2 name="JobID" id="D" precision="9" />
> </Fields>
> </MyClass>
>
> Is there a way to instruct the serializer using Attributes or some
> other to not add a tag for the myFields array?
>
>
> Thanks,
> Yash



 
Reply With Quote
 
Family Tree Mike
Guest
Posts: n/a
 
      31st Mar 2010


"Yash Ganthe" wrote:

> Hi,
>
> I have the follwoing class structure:
>
> public class MyClass {
> public Fields Fields;
> }
>
> public partial class Fields {
>
> public BaseField[] myFields;
>
> }
>
> [System.Xml.Serialization.XmlInclude(typeof(DerivedField))]
> public class BaseField
> {
> //some properties
> }
>
> [XmlRoot("derived")]
> public partial class DerivedField : BaseField {
> }
>
> All the classes are tagged as serilizable.
> When I XMLSerialize an object of type MyClass, here is what I get:
> <MyClass>
> <Fields>
> <myFields>
> <derived name="JobID" id="A" precision="9" />
> </myFields>
> </Fields>
> </MyClass>
>
> The serializer adds an XML element for every public property, which is
> why myfields appears as a tag in the XML.
> This is undesirable in my application as I would like the elements in
> myFields[] to get serialized directly under <Fields> as :
> <MyClass>
> <Fields>
> <derived name="JobID" id="A" precision="9" />
> <derived name="JobID" id="B" precision="9" />
> <derived1 name="JobID" id="C" precision="9" />
> <derived2 name="JobID" id="D" precision="9" />
> </Fields>
> </MyClass>
>
> Is there a way to instruct the serializer using Attributes or some
> other to not add a tag for the myFields array?
>
>
> Thanks,
> Yash
> .
>


You can use XmlIgnore as in the following:

public partial class Fields
{
[XmlIgnore()]
public BaseField[] myFields;
}


Mike
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Configuring XML Serialization to not add an element Yash Ganthe Microsoft Dot NET Framework 2 31st Mar 2010 05:01 PM
Control XML Serialization element name =?Utf-8?B?SGFyZHkgV2FuZw==?= Microsoft C# .NET 3 12th Apr 2007 03:54 PM
Using XmlRoot attribute for Xml Serialization only works for thefirst element Ympostor Microsoft C# .NET 3 11th Sep 2006 10:23 AM
Using XmlRoot attribute for Xml Serialization only works for thefirst element Ympostor Microsoft C# .NET 0 30th Aug 2006 11:21 AM
conditional xml element serialization during runtime Philipp Sumi Microsoft C# .NET 0 28th Jan 2004 08:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:56 AM.