Design Strongly typed object from XML

前端 未结 5 1487
Happy的楠姐
Happy的楠姐 2020-12-18 04:56

I have a couple of XML files that I need to work with, and I\'ve always used the XElement objects and pulled the data via the attribute name or the XElement\'s value.

<
相关标签:
5条回答
  • 2020-12-18 05:31

    If you're able to change the XML format to suit your needs, one way I know of is using WSDL.

    0 讨论(0)
  • 2020-12-18 05:35

    You could put annotations on your class and then use an XmlSerializer to serialize/deserialize your class instances to/from XML - if you need a more customized approach have your class also implement IXmlSerializable then put that serialization/deserialization code within the class.

    0 讨论(0)
  • 2020-12-18 05:38

    Check out Linq to XSD:

    The LINQ to XSD technology provides .NET developers with support for typed XML programming. LINQ to XSD contributes to the LINQ project (.NET Language Integrated Query); in particular, LINQ to XSD enhances the existing LINQ to XML technology.

    0 讨论(0)
  • 2020-12-18 05:43

    You can use xsd.exe (which installs with the windows SDK, but in various locations depending on version - mine is currently in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin) to generate and xsd from the file, or a cs code file.

    For example:

    xsd.exe myFile.xml
    

    will get you myFile.xsd in the location of the xml file.

    Then,

    xsd.exe myFile.xsd /c 
    

    will get you a .cs file with the classes defined. Try xsd.exe /? for full options (you can specify namespaces, etc. as well).

    0 讨论(0)
  • 2020-12-18 05:45

    If you have a schema for the XML you can generate classes using tools such as xsd.exe (windows SDK), svcutil.exe (WCF) or (my personal preference) an open source alternative (Xsd2Code).

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