About xml based on linq to XML API

T

Tony Johansson

Hello!

If I have this XML expression
XElement xElement =
new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName","Rattz")));

how does it come that Element LastName is not a child to FirstName
when BookParticipant is a child to element BookParticipants.

//Tony
 
M

Markus Springweiler

Tony ,
If I have this XML expression
XElement xElement =
new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName","Rattz")));

how does it come that Element LastName is not a child to FirstName
when BookParticipant is a child to element BookParticipants.

Just look at the closing paranthesis: "type", "FirstName" and "LastName"
just contain one single data element, while BookParticipant contains all
these 3 as an open array parameter:

new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName","Rattz")
)
)
 
M

Markus Springweiler

Addendum,
Just look at the closing paranthesis: "type", "FirstName" and "LastName"
just contain one single data element, while BookParticipant contains all
these 3 as an open array parameter:

new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName","Rattz")
)
)

More concrete: "BookParticipants", "type", "FirstName" and "LastName" use
this constructor:
http://msdn.microsoft.com/en-us/library/bb341343.aspx

While "BookParticipant" uses this constructor instead:
http://msdn.microsoft.com/en-us/library/bb302741.aspx
 
M

Martin Honnen

Tony said:
If I have this XML expression
XElement xElement =
new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName","Rattz")));

how does it come that Element LastName is not a child to FirstName
when BookParticipant is a child to element BookParticipants.

Well
new XElement("BookParticipant", ...)
is passed as an argument to the new XElement("BookParticipants", ...)
call while
new XAttribute("type", "Author")
new XElement("FirstName", "Joe")
new XElement("LastName","Rattz")
are all passed as arguments to the new XElement("BookParticipant", ...)
call. That way 'type' becomes an attribute of 'BookParticipant' and both
'FirstName' and 'LastName' become children of 'BookParticipant'.

See
http://msdn.microsoft.com/en-us/library/system.xml.linq.xcontainer.add.aspx
and http://msdn.microsoft.com/en-us/library/bb943882.aspx
 
M

Michael C

Tony Johansson said:
Hello!

If I have this XML expression
XElement xElement =
new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe"),
new XElement("LastName","Rattz")));

how does it come that Element LastName is not a child to FirstName
when BookParticipant is a child to element BookParticipants.

Are you getting sick of seeing a list of presidents every second page yet?
;-)

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top