1. Paul Shannon
  2. SimpleXml



SimpleXml is simple dynamic xml parsing.

Install with NuGet: install-package SimpleXml

Or download dll from the Downloads tab.

Read and Write Xml:

var x =
  @"<root attr="attrvalue">

x.root.child == "nodevalue"
x.root.attr == "attrvalue"

x.root.child = "newnodevalue";
x.root.attr = "newattrvalue";

Read and Write Multiple Nodes:

var x =

x.root.node == IEnumerable containing "value1" and "value2"
x.root.node = "new value"

Read and Write Multiple Nodes With Child Nodes:

var x =

x.root.node.child == IEnumerable containing "value1" and "value2"
x.root.node.child = "new value"

Selecting Nodes By Attribute Value:

var x =
      <node name="a">value1</node>
      <node name="b">value2</node>
      <node name="c">value3</node>

x.root.node.First("name", "b") == "value2"
x.root.node.FirstOrDefault("name", "b") == "value2"
x.root.node.Where("name", "b") == IEnumerable containing "value2"

Selecting Nodes By Attribute Value With Child Nodes:

var x =
      <node name="a"><child>value1</child></node>
      <node name="b"><child>value2</child></node>
      <node name="c"><child>value3</child></node>

x.root.node.First("name", "b").child == "value2"
x.root.node.FirstOrDefault("name", "b").child == "value2"
x.root.node.Where("name", "b").child == "value2"

Load From File:

var stream = File.Open("path\to\file", FileMode.Open);
dynamic x = new SimpleXml(stream);

Save To File:

var stream = File.Open("path\to\file", FileMode.Create);

Ignores Namespaces:

var x =
  @"<root xmlns="http://kevinberridge.com/namespacessuck" xmlns:t="http://kevinberridge.com/namespacesreallysuck">

x.root.child == "value"

Special Case For Leaf Nodes With Attributes:

var x =
      <child attr="attrvalue">childvalue</child>

x.root.child.text == "childvalue"
x.root.child.attr == "attrvalue"

Written by Kevin Berridge

Licensed under the MIT License