PDA

View Full Version : LINQ to XML examples?


e-x
03-06-2012, 05:16 AM
Just a shot in the dark here, but does anyone have any examples of Linq in maxscript they could share? I've been looking into it, but with my limit knowledge of dotnet, I'm not getting very far... :banghead:

Here is the C# example I've been trying to convert (I don't even know if it can be converted...) source. (http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx#Y4746)

XDocument srcTree = new XDocument(
new XComment("This is a comment"),
new XElement("Root",
new XElement("Child1", "data1"),
new XElement("Child2", "data2"),
new XElement("Child3", "data3"),
new XElement("Child2", "data4"),
new XElement("Info5", "info5"),
new XElement("Info6", "info6"),
new XElement("Info7", "info7"),
new XElement("Info8", "info8")
)
);

Any help would be appreciated.

lo
03-06-2012, 07:36 AM
wasn't easy, but...

dotnet.loadassembly "System.Xml.Linq"

fn getXName s = (dotnetClass "System.Xml.Linq.XName").get s

dotnetobject "System.Xml.Linq.XDocument" #( \
dotnetobject "System.Xml.Linq.XComment" "This is a comment",
(dotnetobject "System.Xml.Linq.XElement" (getXName "Root") \
#((dotnetobject "System.Xml.Linq.XElement" (getXName "Child1") (getXName "data1")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child2") (getXName "data2")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child3") (getXName "data3")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child4") (getXName "data4")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child5") (getXName "data5")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child6") (getXName "data6")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child7") (getXName "data7")))
)
)

LoneRobot
03-06-2012, 09:48 AM
Nice one Rotem, I looked into this a while back but it was mostly from the dotnet angle. If anyone is interested I wrote this (http://lonerobot.net/?p=971) article about what I found.

lo
03-06-2012, 02:35 PM
Great article, Pete. Thanks for sharing.

e-x
03-06-2012, 02:51 PM
wasn't easy, but...

Thank you for the help! I gave up trying and switched to using the regular xmlDocument, but I'll re-investigate Linq now that I have your code to reference.

@Pete - I read your article looking into Linq and maxscript. Your stuff is great, but most of it is over my head!

denisT
03-06-2012, 03:02 PM
wasn't easy, but...

dotnet.loadassembly "System.Xml.Linq"

fn getXName s = (dotnetClass "System.Xml.Linq.XName").get s

dotnetobject "System.Xml.Linq.XDocument" #( \
dotnetobject "System.Xml.Linq.XComment" "This is a comment",
(dotnetobject "System.Xml.Linq.XElement" (getXName "Root") \
#((dotnetobject "System.Xml.Linq.XElement" (getXName "Child1") (getXName "data1")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child2") (getXName "data2")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child3") (getXName "data3")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child4") (getXName "data4")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child5") (getXName "data5")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child6") (getXName "data6")),
(dotnetobject "System.Xml.Linq.XElement" (getXName "Child7") (getXName "data7")))
)
)

we can write this a little cleaner:

dotnet.loadassembly "System.Xml.Linq"

fn XDocument value = dotnetobject "System.Xml.Linq.XDocument" value
fn XComment str = dotnetobject "System.Xml.Linq.XComment" str
fn XName str = (dotnetClass "System.Xml.Linq.XName").get str
fn XElement name value = dotnetobject "System.Xml.Linq.XElement" (XName name) value

doc = XDocument \
#(
XComment "This is a comment",
XElement "Root" \
#(
(XElement "Child1" 1),
(XElement "Child2" 2),
(XElement "Child3" 3),
(XElement "Child4" 4),
(XElement "Child5" 5),
(XElement "Child6" 6),
(XElement "Child7" 7)
)
)


the second argument in XElement constructor can be System.Object. so let max do the casting :)

e-x
03-06-2012, 09:25 PM
I have some more questions with this.

When I use Denis' example and try to get the elements using: ele = doc.Elements(XName "Child1") it returns a weird value: dotNetObject:System.Xml.Linq.XContainer+<GetElements>d__11
and I can't seem to do anything with it, and there isn't any documentation on what it is.

But when I use ele = doc.root.Element(XName "Child1") it returns dotNetObject:System.Xml.Linq.XElement which is helpful, but not that helpful since it only returns a single element, if I'm correct.

So what am I doing wrong? How can I get the elements back out of the XML?

Nysuatro
11-07-2012, 01:25 PM
Any solution to e-x question?
Or will I just have to collect all the items through a for loop?

denisT
11-07-2012, 02:08 PM
Any solution to e-x question?
Or will I just have to collect all the items through a for loop?
what is the question? how to use System.Xml.Linq.XContainer+<GetElements>?

CGTalk Moderation
11-07-2012, 02:08 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.