-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicNode.cs
More file actions
41 lines (34 loc) · 1.07 KB
/
DynamicNode.cs
File metadata and controls
41 lines (34 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Dynamic;
using System.Linq;
using System.Xml.Linq;
namespace DynamicXml
{
public class DynamicNode : DynamicObject
{
private readonly XElement _currentElement;
public DynamicNode(XElement currentElement)
{
_currentElement = currentElement;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var nodes = _currentElement.Elements().Where(e => e.Name == binder.Name);
if (nodes.Count() > 1)
result = nodes.Select(n => new DynamicNode(n)).ToArray();
else if (nodes.Count() == 1)
result = new DynamicNode(nodes.First());
else
throw new ArgumentException("No node named " + binder.Name);
return true;
}
public static implicit operator string(DynamicNode dynamicNode)
{
return dynamicNode.ToString();
}
public override string ToString()
{
return _currentElement.Value;
}
}
}