How do I map XML to C# objects

前端 未结 9 1833
旧巷少年郎
旧巷少年郎 2020-11-27 13:37

I have an XML that I want to load to objects, manipulate those objects (set values, read values) and then save those XMLs back. It is important for me to have the XML in the

相关标签:
9条回答
  • 2020-11-27 13:49

    LINQ to XML is very powerful if you're using .net 3.5, LINQ to XSD may be useful to you too!

    0 讨论(0)
  • 2020-11-27 13:49

    Use xsd.exe command line program that comes with visual studio to create class files that you can use in your project/solution, and the System.Xml.Serialization namespace (specifically, the XmlSerializer class) to serialize/deserialze those classes to and from disk.

    0 讨论(0)
  • 2020-11-27 13:49

    We have created a framework which can auto-generate C# classes out of your XML. Its a visual item template to which you pass your XML and the classes are generated automatically in your project. Using these classes you can create/read/write your XML.

    Check this link for the framework and Visual C# item template: click here

    0 讨论(0)
  • 2020-11-27 13:52

    I agree xsd is really crap... But they made another version that hardly anyone knows about. Its called xsd object generator. Its the next version and has way more options. It generates files from XSD and works fantastic. If you have a schema generator like XML spy; create an xsd from your xml and use this tool. I have created very very complex classes using this tool. Then create partial classes for extra properties\methods etc, then when you update your schema you just regen your classes and any edits persist in your partial classes.

    http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7075

    0 讨论(0)
  • 2020-11-27 14:00

    using System.Xml.Serialization; this namespace has all the attributes you'll need if you want to map your xml to any random object. Alternatively you can use the xsd.exe tool

    xsd file.xsd {/classes | /dataset} [/element:element] [/language:language] [/namespace:namespace] [/outputdir:directory] [URI:uri] which will take your xsd files and create c# or vb.net classes out of them.

    http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.71).aspx

    0 讨论(0)
  • 2020-11-27 14:03

    xsd.exe from Microsoft has a lot of bugs :| Try this open source pearl http://xsd2code.codeplex.com/

    0 讨论(0)
提交回复
热议问题