Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing to seek some guide, as I am currently struggling, in how can I create the following XML using the XElement class,

XML
HTML
<job> 
 <to> 
 <target_type>#######</target_type>
 <targets>
 <target>#####</target> 
 </targets>  
 </to> 
 </job> 

C#
XElement xeRoot = new XElement("job");
XElement xeSendTo = new XElement("to");
            xeSendTo.Add(new XElement("target_type", "email_address"),
             
            xeSendTo.Add(new XElement("targets"),
                         new XElement("target", "ma@domain.com")));
            xeRoot.Add(xeSendTo);


I am currently experiencing --> The best overloaded method match for 'System.Xml.Linq.XContainer.Add(params object[])' has some invalid arguments --> error.

Any advice, would be very much helpful.
Many thanks.
Posted
Updated 25-Sep-14 0:52am
v2

1 solution

try this

C#
XElement xeRoot = new XElement("job");
           XElement xeSendTo = new XElement("to");
           xeSendTo.Add(new XElement("target_type"));
           xeSendTo.SetElementValue("target_type", "#######");
           XElement xeSendTo1 = new XElement("targets");
           xeSendTo1.Add(new XElement("target"));
           xeSendTo1.SetElementValue("target", "##### ");
           xeSendTo.Add(xeSendTo1);
           xeRoot.Add(xeSendTo);


good luck ;-)
 
Share this answer
 
v2
Comments
george4986 25-Sep-14 6:52am    
reference

http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement(v=vs.110).aspx
miss786 25-Sep-14 6:56am    
Thank you for your response. I am trying to get the <target> tag under the <targets> tag. I am little unclear how to do this and would you know a specific method within XElement class, I could use to achieve this. Many thanks.
george4986 25-Sep-14 6:59am    
do u need value inside tag like search?
miss786 25-Sep-14 7:03am    
I would like the "target_type" & "target" nodes to have values. Thanks.
george4986 25-Sep-14 7:13am    
updated the solution plz check

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900