[IronPython] XmlDocument or XDocument in Python/Silverlight

Jonathan Slenders jonathan at slenders.be
Wed Jun 18 02:57:23 PDT 2008


2008/6/18 Jonathan Slenders <jonathan at slenders.be>:

> How does IronPython receive the results of GetDescendants() or Elements()
> from the Linq library.
>
> PS C:\Documents and
> Settings\Jonathan\Desktop\IronPython-2.0B3-Bin\IronPython-2.0B3> .\ipy.exe
> IronPython 2.0 Beta (2.0.0.3000) on .NET 2.0.50727.1433
> Copyright (c) Microsoft Corporation. All rights reserved.
> >>>
> >>> import clr
> >>> clr.AddReference('System.Xml.Linq')
> >>> from System.Xml.Linq import XDocument
> >>> document = '<root><a>aaa</a><b>bbb</b><a>ccc</a></root>'
> >>> XDocument.Parse(document).Element('root').Element('a')
> <System.Xml.Linq.XElement object at 0x0000000000000031 [<a>aaa</a>]>
> >>> XDocument.Parse(document).Element('root').Elements('a')
> <System.Xml.Linq.XContainer+<GetElements>d__11 object at 0x0000000000000033
> [System.Xml.Linq.XContainer+<GetElements>d__11]>
> >>> for i in XDocument.Parse(document).Element('root').Elements('a'):
> ...     print i
> ...
> >>>
>
> The for loop doesn't return any result, but it also doesn't raise an
> exception. The MSDN documentation has only examples for c# which all use the
> "from ... select ...." syntax from Linq which isn't available here.
>
> Jonathan
>



And another question, which is rather Silverlight then IronPython related. I
hope it's okey to ask it here.
How should I create a TextNode from Silverlight with IronPython code? In
javascript, one would:

node = document.createTextNode("text");
element.appendChild(node);

According to the MSDN documentation, the HtmlDocument class doesn't have a
createTextNode, and I can't find it anywhere else. Michael his book has been
extremely useful for me, but he uses innerHtml which i really don't like.
InnerHtml is is an old-style method for DOM-manipulation, and unsafe by
definition. (it allows you to insert HTML code)



So far, I worked around my previous problem by using the NextNode property,
which works fine. Though not as nice as iterators would be:


def insert_rows(rows):
        html_table =
HtmlPage.Document.GetElementById('mysql-api-test-table')
        row = XDocument.Parse(rows).Element('table').Element('row')

        while row != None:
                html_row = HtmlPage.Document.CreateElement('tr')
                field = row.Element('field')

                 while field != None:
                        html_field = HtmlPage.Document.CreateElement('td')

#html_field.Children.Add(HtmlPage.Document.CreateTextNode(field.Value)) #
Doesn't exist??
                        html_row.AppendChild(html_field)

                        field = field.NextNode

                html_table.AppendChild(html_row)
                row = row.NextNode

Thank you

Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20080618/1c0a37fe/attachment-0001.htm>


More information about the Users mailing list