XmlSerialization to XmlNode

I have a schema that I load into classes that mirror the schema using
the XmlSerialization. The schema contains an xsd:Any that I load into
'Extra' as follows:

[XmlElement(ElementName="Extra")] public XmlNode Extra { get { return
extra;} set { extra = value;}}

However, if I have:

<Something>
   <Extra>
       <a>something</a>
       <b>another thing</b>
   </Extra>
</Something>

I would expect Extra to contain an XmlNode of :

   <Extra>
       <a>something</a>
       <b>another thing</b>
   </Extra>

But it doesn't : it contains :

<a>something</a>

Is there any attributes etc I can set to get the full element to be
retained ?

TIA

Dominic
0
2/13/2005 10:34:49 PM
dotnet.xml 7266 articles. 0 followers. Follow

1 Replies
604 Views

Similar Articles

[PageSpeed] 43

Yes, when I use

     // [XmlElement(ElementName="Extra")]
    [XmlAnyElement]
    public System.Xml.XmlElement Any;

I get the behavior you seek.
note: the attribute is XmlAnyElement, not XmlElement.  Also, omit the name.


If you want to allow more than one element, like so:

 <Something>
   <Extra>
       <a>something</a>
       <b>another thing</b>
   </Extra>
   <foo/>
   <bar>
      <blah/>
    </bar>
 </Something>

then specify an array:


    [XmlAnyElement]
    public System.Xml.XmlElement[]  Any;



-Dino


"Dominic Messenger" <dmessenger@verdantsys.com> wrote in message 
news:9d6bd82b.0502131434.6bbe3c81@posting.google.com...
>I have a schema that I load into classes that mirror the schema using
> the XmlSerialization. The schema contains an xsd:Any that I load into
> 'Extra' as follows:
>
> [XmlElement(ElementName="Extra")] public XmlNode Extra { get { return
> extra;} set { extra = value;}}
>
> However, if I have:
>
> <Something>
>   <Extra>
>       <a>something</a>
>       <b>another thing</b>
>   </Extra>
> </Something>
>
> I would expect Extra to contain an XmlNode of :
>
>   <Extra>
>       <a>something</a>
>       <b>another thing</b>
>   </Extra>
>
> But it doesn't : it contains :
>
> <a>something</a>
>
> Is there any attributes etc I can set to get the full element to be
> retained ?
>
> TIA
>
> Dominic 


0
dinoch1 (221)
2/14/2005 10:57:02 PM
Reply:

Similar Artilces:

XmlSerializer Collection with Collections
Hello I got this working but it is not how I really want it, basically I have an xml file which has a root of <test> and can be filled with 3 different types of <question> elements with different attributes, all share a base set of 4, one of the question types can have children with <option> elements, this is how the xml looks after serialization.... If you notice there is an extra <SelectionList> around the <option>'s in the final <question>, how can I write my class so I don't have this extra <SelectionList> element....my class follows, you ...

XmlSerializer
I'm having a basic problem with the XmlSerializer & i'm at my wits end trying to sort it out i have a basic clas <Serializable()> Class Tes Public Name as Strin End Clas when this serializes i wan <Test><Name>Value</Name></Test the problem is if i use the XmlSerializer clas Dim t as Tes t.Name="Value Dim sb As New StringBuilde Dim sw As New StringWriter(sb Dim Serializer As New XmlSerializer(Message.GetType Serializer.Serialize(sw, t Serializer = Nothin Console.Write(sb.ToStr...

XmlSerializer and shared objects (or How to generate IDREFs using XmlSerializer)
This is a multi-part message in MIME format. ------=_NextPart_000_007F_01C34AC1.11D4FB20 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I am trying to find a solution that will allow me to use XmlSerializer = to serialize/deserialize a collection of objects where a given object is = shared between two or more other objects, and not create duplicate XML = representations of the shared object, but instead use IDREFs to refer to = the shared object. The XML I'm trying to produce is as follows (where "href" is an IDREF): <...

XmlSerializer and inherited objects
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting FileMessage or ChatMessage. So how to get that object and use it I have written following code for serialization public void Send(Message message) { NetworkStream netWorkStream=null; try { XmlSerializer serializer=new XmlSerializer(message.GetType()); netWorkStream=new NetworkStream(_clientSocket); Stream stream=(Stream)netWorkStream; serializer.Serialize(stream,message); } finally { netWorkStrea...

XMLSerializer Serialize
Iam trying to create a xml file by serializing a custom business object. Iam using a xmltextwriter, but the problem there are invalid characters at the end of the generated file anyone knows a way around it ? tia Dim myStream As New IO.MemoryStream Dim myXMLWriter As New System.Xml.XmlTextWriter(myStream, System.Text.Encoding.UTF8) myXMLWriter.Formatting = System.Xml.Formatting.Indented Dim myShipments As OrderShipments myShipments = CType(myOrder.OrderAddresses(0), COrderAddress).Shipments myShipments.ToXML(myXMLWriter) ...

XmlSerializer help
Hi, I have a class: public class Note { public string name; public string information; } And i Serialize this class, but want to Serialize it to an XmlDocument which will contain many of these elements. How do i do this? Thank you, Tim. Timothy, take a look at Chris Lovett's XmlNodeWriter on www.gotdotnet.com [0]. Simply pass the node where you want to insert the serialized XML into the XmlDocument to an XmlNodeWriter and pass the XmlNodeWriter to the Serialize method. -- HTH Christoph Schittko [MVP] Software Architect, .NET Mentor [0] http://gotdotnet.com/Communit...

Problems with XmlSerializer and namespaces
I have an object, Teacher, that has a namespace. Let's call it http://foo. Teacher has a child object called Name, composed of String first, middle, last, etc. Problem I have is when I try to deserialize it. When I deserialize a single Teacher, it works when I put the namespace on the Teacher element. However, if I have just the namespace on Teacher, it will deserialize fine as Teacher. *However*, if I try to deserialize an ArrayOfTeacher, Teacher.Name doesn't deserialize (it's null); without the array, it's OK. What I wound up doing was serializing both a Teacher ...

XmlSerializer and ICollection
Hello all, I have two simple classes: Item and ItemCollection. Item stores a label for the item and an instance of ItemCollection for all child items. ItemCollection just stores a collection of Items. ItemCollection implements System.Collections.ICollection. When I use XmlSerializer to serialize an instance of ItemCollection, I am unable to control the element names used to contain the item collection and the items therein. Applying XmlRootAttribute to the ItemCollection class causes an exception because it implements ICollection. I have also tried implementing IXmlSerializer in my ItemColl...

XmlSerializer question
Hi, I use XmlSerializer to serialize xml string. It escapes xml special charaters, like < and > to < and >. How can I stop XmlSerializer to escape these charaters? Thanks in advance!!! Cindy Cindy wrote: > I use XmlSerializer to serialize xml string. It escapes xml special > charaters, like < and > to < and >. How can I stop XmlSerializer to escape > these charaters? If the XmlSerializer didn't escape the characters < and & (and for compatibility >), then the result would not be well-formed XML. See section 2.4 of the XML specification [1]...

XmlSerializer and DefaultValue?
Hello, I'm currently playing around with the XmlSerializer to replace my old handmade Xml parsing code, but am having a few small issues with default values and derived classes. Say we have classes A, B and C: abstract class A { protected int a_value = 1; [XmlAttribute("value"), DefaultValue(1)] public int AValue { get { return a_value; } set { a_value = value; } } } class B : A { .... } class C : A { .... } Now, I want class B and C to have different default values, more precisely, I want B to have 1 as default value and C to have Int32.MaxValue. But I ...

XmlSerializer question...
Hi All, When using XmlSerializer is there anyway of keeping "xsi", "xsd" namespaces and PI appearing in the xml file ? <?xml version="1.0"?> <myClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="Test" Visible="false" /> Just want a simple clean one like the following... <ColumnInfo Name="Test" Visible="false" /> Thanks, Maersa wrote: > When using XmlSerializer is there anyway of keeping "xsi", "xsd"...

How to XMLSerialize Collection Class
Hi, I have a collection class where I've implemeneted the ICollection Interface. Here is a small code segment. public class PageList : ICollection, IComparer, IEnumerable, IList { protected ArrayList _pages; public PageList() { _pages = new ArrayList(); } #region IList Members public object this[int index] { get { if (index > _pages.Count) { return (Page)null; } else { return (Page)_pages [index]; } } set { } } public int Add(object objPage) { int arrayIndexAdded = -1; arrayIndexAdded = _pages.A...

XMLSerializer for javascript
Hello, Can XMLSerializer be called from javascript ? How can I use XMLSerializer on the client side, and on the server side ? I need an example, please. Thanks :) Mr. x wrote: > Can XMLSerializer be called from javascript ? Not in javascript, but in JScript.NET. > How can I use XMLSerializer on the client side, and on the server side ? XmlSerializer (if you're talking about System.Xml.Serialization.XmlSerializer class of .NET) requires .NET environment. On server side it's usually ASP.NET, on client side - Windows forms or Web Forms. -- Oleg Tkachenko http://www.tkachen...

XmlSerializer Problem
Hi all, When I run this code: XmlSerializer theXS; try { theXS = new XmlSerializer(typeof(TableInfo)); } catch( System.Exception eAll ) { string junk = eAll.Message; return; } I get this exception: File or assembly name 5wpkczfu.dll, or one of its dependencies, was not found If I change the type of the object I'm trying to serialize I get references to other obscure missing dll files... none of which can be found on Google, oddly enough. Any thoughts or suggestions would be appreciated. - Nick Hi Nick, Is the TableInfo referencing any other classes that you wrote? If ...

XmlSerializer constructor
I'm having trouble constructing the XmlSerializer without getting a Configuration Exception(Only elements allowed: Line 3) in Dev Studio 2003. When I remove the <system.diagnostics> node from my app.config I don't have any issues but I wanted to keep the node. Please let me know if anyone has any ideas. app.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.diagnostics> <assert assertuienabled="true" logfilename="assert.txt"/> <switches> <add name="General"...

XmlSerializer is ambiguous in the namespace System.Xml.Serialization
error: 'XmlSerializer' is ambiguous in the namespace 'System.Xml.Serialization' In VS 2010 IDE at design time of an asp.net 4.0 site. Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.Data Imports System.Data.SqlClient Imports System.Xml Imports System.Xml.Serialization Dim ser As New XmlSerializer(s.GetType) <- error reported here The bin file has system.xml.serialzation.xml and system.xml.serialzation.dll If I delete the dll the error goes away, but when I build then dll is put back into bin and...

XMLSerializer
The simple constructors of XMLSerializer class caches the typed serializer once it is generated. (Probably as you know, other XMLSerializer constructors will result in a memory leak because they will generate the assembly each time instead of using the cached one) a) When an XMLSerializer is used from ASP.NET, where exactly the generated typed serializer is getting cached? What is the probability/crieteria for the object in the cache may get distroyed/invalidated? b) Can we assume that once the XMLSerializer is initialized with the constructor XMLSerializer (sometype), all further X...

XmlSerializer and XslTransform
I'm making a 6-tiered application that is using Xml to tie the various layers together in a very clean manner. I'm also trying to do this on the 1.1 Framework although I'm willing to switch to the 1.0 Framework if need be. One layer, which I call a Data Persistence Layer, uses the System.Xml.Serialization.XmlSerializer to transform our business/domain objects into the appropriate Xml format. The layer below that is what I call a Data Transformation/Translation Layer. This layer converts the Xml from the Persistence layer into the appropriate format for storage (Sql server, fi...

XmlSerializer and TypeLoadException
Hello, I'm trying to deserialize an XML document into a objects with the XmlSerializer which works fine in my NUnit test suit but fails with a TypeLoadException in the server. The difference between the server and the NUnit test suit is that the server will load the assembly (Flux.Grease.Services.dll) as a plugin in its own AppDomain whereas the NUnit test suit reference the assembly in the usual way. The code that performs the deserialization reside in the Flux.Grease.Services assembly and looks like this: XmlSerializer serializer = new XmlSerializer(typeof(CitectExport)); citectExport...

XmlSerializer, XmlAnyElement and CDATA
(VS.NET 2K3, C#) Hi, I need to serialize/deserialize an element using XmlSerializer that can contain any valid XML including a CDATA section. [XmlRootAttribute] public class Test { [XmlAnyElement] public XmlElement Anything; } This doesn't work with an XML like the following: <Test><![CDATA[ msgbox "Hi222"]]></Test> However this works fine: <Test><Wrap><![CDATA[ msgbox "Hi222"]]></Wrap></Test> Unfortunately in my case the XML between <Test> comes from outside of my application and can contain virtually anything...

XmlSerializer and TypeDelegator
given: class Foo { public int x = 1; public int y = 2; } this works: XmlSerializer x = new XmlSerializer(typeof(Foo)); x.Serialize(Console.Out, new Foo()); "<Foo> <x>1</x> <y>2</y> </Foo>" while this does not work: XmlSerializer x = new XmlSerializer(new TypeDelegator(typeof(Foo))); x.Serialize(Console.Out, new Foo()); "<Foo/>" Why? does anyone know of anything I can do to make the TypeDelegator work? Could there be something in TypeDelegator I need to override? ...

XmlSerializer and MemoryStream
I am using some Microsoft examples that: 1. Serialize an object using XmlSerializer and write a file out to the harddrive. 2. Read back in the file using XmlDocument.Load and populate a string. What I would like to do is use MemoryStream to do this instead of writing to the harddrive, but I am lost in the readers, writers etc. Can someone show me how to to this using a memory stream. thanks grs // Code to accept eConnectType object and write to a disk file public void WriteObjectsToFile(string fileName,eConnectType objectToSerialize) { try { XmlSerializer xmlSerializer = new XmlSerializer(o...

XmlSerializer and ISerializable
Can I control XmlSerializer's serialization/deserialization process by implementing ISerializable on the class I'm serializing? I'm doing exactly this with the SoapFormatter and I was wondering if it is the same for the XmlSerializer. Hi Paul, Customizing the xml serialization is different from SoapFormatter. Generally, we use attributes to customize the serialization process. The SoapFormatter also use these attributes, but it can utilize the GetObjectData as well. Here I suggest you take a look at the following article. It will be helpful. http://msdn.microsoft.com/...

XmlSerializer and IEnumerable
I'm trying to serialize a class with XmlSerializer. This class implements the IEnumerable interface. I implemented the IEnumerable interface for reasons other than Xml serialization. However I find that the XmlSerializer, because I have an IEnumerable interface on this class, wants to ignore the public properties of this class and simply serialize an array of objects. I wand XmlSerializer to ignore the IEnumerable interface that I have implemented on this class. How do I do that? Regards, Bob Rundle Have you marked your properties with [XmlAttribute] and/or [XmlElement]? Those are...

XMLSerializer: Schema and Datasets
I am looking into the possibilities that exist from utilizing XML Schema documents to store the structure of datasets that don't change frequently for access in distributed applications. I discovered the XMLSerializer does not support: 1. Any of the simple type restriction facets besides enumeration 2. Namespace based wildcards 3. Identity constraints 4. Substitution groups 5. Blocked elements or types (see faq in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml01202003.asp) What are the specific W3C Schema attributes that datasets do not support? ...