Notifications
Clear all

[Closed] LINQ to XML examples?

 e-x

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.

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.

8 Replies
 lo1

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"))) 
	)	
)
2 Replies
 e-x
(@e-x)
Joined: 1 year ago

Posts: 0

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)
Joined: 1 year ago

Posts: 0

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

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.

 lo1

Great article, Pete. Thanks for sharing.

 e-x

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?

Any solution to e-x question?
Or will I just have to collect all the items through a for loop?

1 Reply
(@denist)
Joined: 1 year ago

Posts: 0

what is the question? how to use System.Xml.Linq.XContainer+<GetElements>?