XML sample
<?xml version="1.0" encoding="utf-8" ?>
<file>
	<header>
		<title attr1="Main">Welcome</title>
	</header>
	<message flag="None">
		<body>Hello, World!</body>
		<parameter>Param1</parameter>
		<parameter>Param2</parameter>
		<parameter>Param3</parameter>
	</message>
</file>
Single XML element access
dynamic xmlElement = new DynamicXmlElement(SimpleXDocument.Root);
Console.WriteLine(xmlElement.header.title);
Console.WriteLine(xmlElement.message.body);
Get XML array element by index
dynamic xmlElement = new DynamicXmlElement(SimpleXDocument.Root);
Assert.AreEqual("Param1", (string)xmlElement.message.parameter[0]);
Assert.AreEqual("Param2", (string)xmlElement.message.parameter[1]);
Assert.AreEqual("Param3", (string)xmlElement.message.parameter[2]);
Get XML array element by enumeration
dynamic xmlElement = new DynamicXmlElement(SimpleXDocument.Root);
foreach (var parameter in xmlElement.message.parameter)
    Console.WriteLine(parameter);
Get length of XML elements array
dynamic xmlElement = new DynamicXmlElement(SimpleXDocument.Root);
Assert.AreEqual(3, xmlElement.message.parameter.Count);
Get attribute of an XML element
dynamic xmlElement = new DynamicXmlElement(SimpleXDocument.Root);
Assert.AreEqual("Main", (string)xmlElement.header.title.attr1);
Assert.AreEqual("None", (string)xmlElement.message.flag);
Optional XML element access
// if "body" element is absent you won't get exception but empty string
Console.WriteLine(xmlElement.message._Opt_body);

Last edited Dec 7, 2012 at 1:03 PM by donnicky, version 2

Comments

No comments yet.