CGTalk > Software > Autodesk 3ds max > 3dsMax SDK and MaxScript
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 03-06-2012, 06:16 AM   #1
e-x
New Member
Alex Utting
Animator
Bethesda Game Studios
USA
 
Join Date: Feb 2003
Posts: 22
LINQ to XML examples?

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...

Here is the C# example I've been trying to convert (I don't even know if it can be converted...) source.

Code:
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.
 
Old 03-06-2012, 08:36 AM   #2
lo
the frequentest!
portfolio
Rotem Shiffman
Tel Aviv, Israel
 
Join Date: Jul 2005
Posts: 2,831
wasn't easy, but...

Code:
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"))) ) )

Last edited by lo : 03-06-2012 at 08:39 AM.
 
Old 03-06-2012, 10:48 AM   #3
LoneRobot
Swami Lama's Lovechild
 
LoneRobot's Avatar
portfolio
Pete Addington
Technical Director
Nexus
United Kingdom
 
Join Date: Dec 2004
Posts: 1,784
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 article about what I found.
__________________
Regards,
Pete

Read my technical blog - lonerobot.net
 
Old 03-06-2012, 03:35 PM   #4
lo
the frequentest!
portfolio
Rotem Shiffman
Tel Aviv, Israel
 
Join Date: Jul 2005
Posts: 2,831
Great article, Pete. Thanks for sharing.
 
Old 03-06-2012, 03:51 PM   #5
e-x
New Member
Alex Utting
Animator
Bethesda Game Studios
USA
 
Join Date: Feb 2003
Posts: 22
Quote:
Originally Posted by lo
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!
 
Old 03-06-2012, 04:02 PM   #6
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,420
Send a message via ICQ to denisT
Quote:
Originally Posted by lo
wasn't easy, but...

Code:
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:
Code:
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
 
Old 03-06-2012, 10:25 PM   #7
e-x
New Member
Alex Utting
Animator
Bethesda Game Studios
USA
 
Join Date: Feb 2003
Posts: 22
I have some more questions with this.

When I use Denis' example and try to get the elements using:
Code:
ele = doc.Elements(XName "Child1")
it returns a weird value:
Code:
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
Code:
ele = doc.root.Element(XName "Child1")
it returns
Code:
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?
 
Old 11-07-2012, 02:25 PM   #8
Nysuatro
Veteran
portfolio
Robbert-Jan Brems
Leuven, Belgium
 
Join Date: Oct 2005
Posts: 80
Send a message via MSN to Nysuatro
Any solution to e-x question?
Or will I just have to collect all the items through a for loop?
__________________
Portfolio
 
Old 11-07-2012, 03:08 PM   #9
denisT
MAX Doctor
 
denisT's Avatar
portfolio
Denis Trofimov
CA, USA
 
Join Date: Jul 2009
Posts: 9,420
Send a message via ICQ to denisT
Quote:
Originally Posted by Nysuatro
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>?
 
Old 11-07-2012, 03:08 PM   #10
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,481
Thread automatically closed

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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 09:44 PM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.