Git Product home page Git Product logo

xmltodynamic's Introduction

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 = @"<?xml version=""1.0"" encoding=""UTF-8""?><blah></blah>";
var dynamicXml = XElement.Parse(xmlString).ToDynamic();

Values

The elements get converted to properties, and their values can be accessed through the Value property.

// <addressBook>
//   <contacts>
//     <contact>
//       <name>Jonathan</name>
//     </contact>
//   </contacts>
// </addressBook>
var firstPersonsName = dynamicXml.contacts[0].name.Value;
// firstPersonsName will be 'Jonathan'

Attributes

Attributes can be accessed through 'index' syntax, as shown below.

// <addressBook>
//   <contacts>
//     <contact id="1">
//       <name>Jonathan</name>
//     </contact>
//   </contacts>
// </addressBook>
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.

// <colors>
//   <blue></blue>
//   <green lightness="1"></green>
//   <green lightness="10"></green>
// </colors>
var darkGreenLightness = dynamicXml.greens[0]["lightness"];
var lightGreenLightness = dynamicXml.greens[1]["lightness"];
// darkGreenLightness will be '1'
// lightGreenLightness will be '10'

xmltodynamic's People

Contributors

jonathanconway avatar bazile avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.