Generics and Duck-Typing XML in .NET?

I work with some representations of XML data instances. I am deserializing objects using .NET serialization, but something in my soul is bothered by the need to write classes to represent XML ... The following is what I LOVE, but I don’t know the syntax or if it is even possible:

Consider the following:

dim xmlObject = SomeXMLFunction() 'where some function returns an object/string representation of xml... xmlObject.SomePropertyDefinedInTheXML = SomeFunction() 

Any suggestions on approaches with this?

+7
generics duck-typing
source share
4 answers

VB.NET allows you to work with XML in a rather intuitive way:

 Sub Serialize() Dim xml = <myData> <someValue><%= someFunction() %></someValue> </myData> xml.Save("somefile.xml") End Sub Sub Serialize2() ' if you get the XML skeleton as a string Dim xml = XDocument.Parse("<myData><someValue></someValue></myData>") xml.<myData>.<someValue>.Value = "test" xml.Save("somefile.xml") End Sub Sub Deserialize() Dim xml = XDocument.Load("somefile.xml") Dim value = xml.<myData>.<someValue>.Value ... End Sub 

Disadvantage: you do not have a strong seal; the Value property always returns a string.

+3
source share

Go and get xsd.exe . It will create XML serialization classes from your schema definition. Automatically!

+4
source share

If you are defining an XML definition (i.e., XSD) than actually writing classes representing XML, this is a good idea (they are called DTOs). This gives you a strongly typed class for encoding, and you get free serialization without having to manually perform error-prone parsing of xml. If so, write classes first, for example.

 [DataContract] public class Book { [DataMember] public string Name {get;set;} [DataMember] public string Author {get;set;} } //Then you can use this code to serialize var xml = DataContractSerializer.Instance.SerializeToString( new Book {Name="A", Artist="B"}); //which will give you something like: 
 <Book> <Name>A</Name> <Author>B</Author> </Book> 
 //You can then [Deserialize][2] it back again with: var book = DataContractDeserializer.Instance.Parse<Book>(xml); 

Here are links to Serializer and Deserializer .

If you have WSDL or XSD, you can use wsdl.exe (or Add a service link in VS.NET) or xsd.exe to generate dto classes for you, as suggested by @DavidSchmitt.

Otherwise, if XSD is not available, I recommend that you take a look at XLinq for another easy way to parse XML.

+3
source share

What you set with duck printing is easy typing, and .net is, at least, static typing, at least until version 3.5.

if you follow the path of these types, and if you want, enter the type of release: classical frameworks => prototyping frameworks => duck typing .

In js, obviously, you can achieve almost all of this, but in C # or vb.net you will find yourself in a classic and bureaucratic way when it comes to types.

You can freely create these types at runtime, but it consumes processing time, and while it is not in memory, it can be very slow.

If necessary, you have two ways (including reflection):

  • You can create these classes, which are likely to be the owner of the property using the property information, and then create the type and paste it into it. You will need to create a place to place, for example, an assembly or module. you will have little or no support from your current methods if you don’t think about an action plan for this and don’t worry about the security issues that it might involve.

  • You can follow the most painful path and use reflection.emit to create your type directly in the CLR, which can provide you with many, many benefits. It may turn out to be sick to do this though.

If you find a way, please, I would like to hear about it, because the duck printed it perfectly. Independent and courageous people must be valued.

Good luck to you

0
source share

All Articles