How to add value between tags using XElement?

I have looked a bunch of XML samples using XDocument and XElement but they all seem to have self closing tags like <To Name="John Smith"/>. I need to do the following:

<To Type="C">John Smith</To>

I thought the following would work and tried to look at the object model of the Linq.XML class, but I'm off just a tad (see line below that is not working)

new XElement("To", new XAttribute("Type", "C")).SetValue("John Smith")

Any assistance on how to get the XML formed properly is appreciated, thanks!

Answers


I'd use:

new XElement("To", new XAttribute("Type", "C"), "John Smith");

Any plain text content you provide within the XElement constructor ends up as a text node.

You can call SetValue separately of course, but as it doesn't return anything, you'll need to store a reference to the element in a variable first.


How about

  new XElement("To", new XAttribute("Type", "C"), "John Smith")

Need Your Help

How do processes work with windows services?

.net windows-services

I have a widows service that is scheduled to run every hour or so. It basically calls a static method in a referenced assembly.

Combine LaTeX with R markdown and pandoc

r latex rstudio

I have created a R markdown (RStudio) document and I would like to have that output in pdf combined with some part (i.e. first page) of my LaTeX document. So basically, I would like to do some bits...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.