LINQ to XML examples?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 03 March 2012   #1
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.

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 March 2012   #2
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"))) 
	)	
)

Last edited by lo : 03 March 2012 at 08:39 AM.
 
Old 03 March 2012   #3
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 March 2012   #4
Great article, Pete. Thanks for sharing.
 
Old 03 March 2012   #5
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 March 2012   #6
Originally Posted by lo: 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
 
Old 03 March 2012   #7
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?
 
Old 11 November 2012   #8
Any solution to e-x question?
Or will I just have to collect all the items through a for loop?
__________________
Portfolio
 
Old 11 November 2012   #9
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 November 2012   #10
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



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 04:46 AM.


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