Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jonathanconway/xmltodynamic
Converts an XML document into a dynamic object
https://github.com/jonathanconway/xmltodynamic
Last synced: about 12 hours ago
JSON representation
Converts an XML document into a dynamic object
- Host: GitHub
- URL: https://github.com/jonathanconway/xmltodynamic
- Owner: jonathanconway
- License: mit
- Created: 2013-06-20T08:22:56.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-10-21T01:33:52.000Z (about 8 years ago)
- Last Synced: 2024-11-13T04:55:45.732Z (about 2 months ago)
- Language: C#
- Size: 13.7 KB
- Stars: 31
- Watchers: 8
- Forks: 16
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# XmlToDynamic
XmlToDynamic converts your XML object into a hierarchy of nested objects, so you can get at values and attributes more easily.
Instead of this:
xelement.Element(XName.Get("contact")).Attribute(XName.Get("firstName")).Value
You can type this:
xelement.contact["firstName"]The object returned is also dynamic, so you can add new properties on-the-fly.
## Usage
To dynamize your XML, simply generate an XElement of it, then call `ToDynamic()` on it.
var xmlString = @"";
var dynamicXml = XElement.Parse(xmlString).ToDynamic();### Values
The elements get converted to properties, and their values can be accessed through the `Value` property.
//
//
//
// Jonathan
//
//
//
var firstPersonsName = dynamicXml.contacts[0].name.Value;
// firstPersonsName will be 'Jonathan'### Attributes
Attributes can be accessed through 'index' syntax, as shown below.
//
//
//
// Jonathan
//
//
//
var firstPersonsId = dynamicXml.contacts[0]["id"];
// firstPersonsId will be '1'### Repeated elements
Whenever repeated elements are encountered, they're put into a container which is named after the element names pluralized.
//
//
//
//
//
var darkGreenLightness = dynamicXml.greens[0]["lightness"];
var lightGreenLightness = dynamicXml.greens[1]["lightness"];
// darkGreenLightness will be '1'
// lightGreenLightness will be '10'