c# 4.0 - XML navigation and reading using C# 4.0 dynamic? -


Is there a DLR-enabled XML navigation and reading class available in .NET 4.0? For example, I have this XML:

  & lt; Foo & gt; & Lt; Bars & gt; Fobar is here & lt; / Bar & gt; & Lt; Bars & gt; Fobar is also here & lt; / Bar & gt; & Lt; Baz & gt; Fobar is not here & lt; / Bar & gt; & Lt; / Foo & gt;  

There is an easy way to navigate through this XML like this:

someDlrEnabledParser.Parse (xmlString); Foreach (var node in doc.foo.bar) {if (node ​​== "fobers is here") DoSomething (); Else DoSomethingElse (); }

I can see many reasons, from the point of viewpoint, beyond the point of view, including namespace, versus elements, difference collection versus single elements, text versus encoding XML, etc. Responsible.

But XML is much more simple about handling and read only, and I have the specialty of working with "bracket and bid soup" that is simple XML to avoid in exchange The sensible default behavior will be ready to accept pre-4.0 in the world

For example, the "dot" operator can examine the attribute names before the name of the sub-name. Or non-archiving tasks will be automatically applied to the first element (like jQuery does).

Is there anything like this in the .NET 4.0 Framework Classroom Library? If not, any recommendations for a good open source project or a sample of a DLR-enabled XML library?

I did a small test implementation:

  public category DynamicXml : Dynamic Object, IEnumerable & lt; XNode & gt; {Private Readonly IEnumerable & lt; XNode & gt; Nodes; Public DynamicXml (parameter XNode [] nodes) {this.nodes = nodes; } Public Override bool TryGetMember (GetMemberBinder Binder, Outside Object Result) {var kids = nodes.OfType & LT; XContainer & gt; () SelectMany (Node = & gt; node.Elements (binder.Name)) Cast. & Lt; XNode & gt; () ToArray (); Results = new dynamics xml (children); Back true; } Public Inimetator & lt; XNode & gt; GetEnumerator () {Return Nodes .Get Animator (); } IEnumerator IEnumerable.GetEnumerator () {return GetEnumerator (); }}  

and example:

  class program {static void key (string [] args) {dynamic dynDoc = new DynamicXml (XDocument.Parse (@ & Lt; foo & gt; Bar & gt; Fiber is here & lt; / bar & gt; Bar & gt; Fobar is here & lt; / bar & gt; & lt; baz & gt; ; Fobar is not here & lt; / foo & gt; ")); foreach (XElement node in dynDoc.foo.bar) {if (node.Value ==" foobar is here ") Console.WriteLine ("found: {0}", node); Else Console.WriteLine ("not found: {0}", node);} console.readkey (true);}}  

However, it seems that the interface between this and some other code is not very good For example, if we want to use a link against the node, we have to explicitly put it on a IEnumerable otherwise our where is a Element will be interpreted as the name.

  ((DynamicXml) DynXml.foo.bar). Where (X => x.Value == "Fobar is here");  

You can apply directly to the dynamic type where , but then you will have to qualify all such lambda:

  foo bar. Where ((FunkLt; XElement, bool & gt;) (x = & gt; x.Value == "FOB is here"))  

He said, if you just The values ​​are removed from an XML tree like this, I think it works fine.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -