Reputation: 10509
I am always bound to write an XML serializer classes to save data to disk in my different applications that use XML files as a structured info storage.
Is there a tool that you can feed an example of an XML structure and it will generate a c-sharp class to work with such xml structure?
For example I want to work with XML store of "projects"
<projectsInfo>
<projectTypes>
<Type>super</Type>
<Type>best</Type>
</projectTypes>
<projects>
<project>
<name>One</name>
<p-type>best</p-type>
</project>
</projects>
</projectsInfo>
And it would generate a Linq-to-XML enabled class to add/remote types and projects instances.
Maybe this is already a commonly solved solution I am just not aware of, or everyone is using a build-in XML serialization of memory objects?
Upvotes: 1
Views: 1766
Reputation: 2123
You can generate classes/schema based on your xml file using the XSD utility provided with visual studio. Once you have the class, you can use the XmlSerializer to serialzie / deserialize files into instances of class. LINQ will work with any property within the class which implements IEnumerable<T>
(any collection).
With .net 4 you can also use the dynamic keyword, to build a dynamic xml wrapper, but im not sure its too comfortable / usefull
Upvotes: 0
Reputation: 6141
If you have an XSD you can generate it with the xsd.exe that you open in the visual studio command line.
More info: http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx
This will auto generate classes from an XSD, and it can generate an XSD from an XML.
Upvotes: 1
Reputation: 21261
c# has the XmlSerializer
class that is designed to serialise objects to XML files.
You can simply annotate your class with various attributes to format the XML. (eg [XmlElement]
,[XmlAttribute]
etc.)
Upvotes: 1