Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

SimpleXml is simple dynamic xml parsing.

Install with NuGet: install-package SimpleXml

Read and Write Xml:

var x =
  @"<root attr="attrvalue">
      <child>nodevalue</child>
    </root>".AsSimpleXml();

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

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

Read and Write Multiple Nodes:

var x =
  @"<root>
      <node>value1</node>
      <node>value2</node>
    </root>".AsSimpleXml();

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

Read and Write Multiple Nodes With Child Nodes:

var x =
  @"<root>
      <node>
        <child>value1</child>
      </node>
      <node>
        <child>value2</child>
      </node>
    </root>".AsSimpleXml();

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

Selecting Nodes By Attribute Value:

var x =
  @"<root>
      <node name="a">value1</node>
      <node name="b">value2</node>
      <node name="c">value3</node>
    </root>".AsSimpleXml();

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 =
  @"<root>
      <node name="a"><child>value1</child></node>
      <node name="b"><child>value2</child></node>
      <node name="c"><child>value3</child></node>
    </root>".AsSimpleXml();

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);
x.Save(stream);

Ignores Namespaces:

var x =
  @"<root xmlns="http://kevinberridge.com/namespacessuck" xmlns:t="http://kevinberridge.com/namespacesreallysuck">
      <t:child>value</child>
    </root>".AsSimpleXml();

x.root.child == "value"

Special Case For Leaf Nodes With Attributes:

var x =
  @"<root>
      <child attr="attrvalue">childvalue</child>
    </root>".AsSimpleXml();

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

Written by Kevin Berridge

Licensed under the MIT License

Recent activity

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.