Specifying Optional child elements when creating a new XElement object

I'm creating an XML "document" programmatically using:

new XElement("Root",
  new XElement("Child", "Child 1"),
  new XElement("Child", "Child 2"),
  new XElement("Child", "Child 3"));

However, I want to add a child element optionally - based on a condition. At 
present I'm doing the following:

bool addOptionalChild = true;

new XElement("Root",
  addOptionalChild ? new XElement("OptionalChild", "Optional Child 1") : 
null,
  new XElement("Child", "Child 1"),
  new XElement("Child", "Child 2"),
  new XElement("Child", "Child 3"));

Is there a neater way to do this?

0
1/9/2009 12:19:58 PM
dotnet.xml 7266 articles. 0 followers. Follow

1 Replies
977 Views

Similar Articles

[PageSpeed] 29

Susan Harris wrote:

> However, I want to add a child element optionally - based on a 
> condition. At present I'm doing the following:
> 
> bool addOptionalChild = true;
> 
> new XElement("Root",
>  addOptionalChild ? new XElement("OptionalChild", "Optional Child 1") : 
> null,
>  new XElement("Child", "Child 1"),
>  new XElement("Child", "Child 2"),
>  new XElement("Child", "Child 3"));
> 
> Is there a neater way to do this?

I don't think so.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
0
mahotrash (1777)
1/9/2009 12:31:03 PM
Reply:

Similar Artilces:

create PO's by reorder #
I have found NO way to creat a PO by the supplier reorder #. I have a potential client whos current program has this ability. Unless I'm off my rocker I have not found a way to do this. Any suggestions or answers would be gladly welcomed. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree"...

Create mailbox that belongs to other domain
Hi, We have two domains under the same forest (domain A and B). We only have one Exchange 2003 server that is currently residing in Domain A. Could Domain B user have a mailbox created from the Exchange 2003 server that is in Domain A ? Thank you. Regards, Zul You would have to run domainprep in domain B and create a RUS for domain B. Once that was done you should be able to create a mailbox for a user in Domain B. Nue "Zul J" <mlist@istar.com.my> wrote in message news:uUHGPU9cGHA.4592@TK2MSFTNGP04.phx.gbl... > Hi, > > We have two domains under the same f...

Using object model to coult the number of used rows
Hi Does any one know how to count the number of used columns and rows in a sheet via the object model I know int m_columncount = m_CurrentWorkSheet.Columns.Count; gives the total columns, but what I need is a count of the used colums. Or do I need to write a manual count i.e. manually count how many are used by by checking each element? thanks Andy Hi Andy, The following will get the number of rows in your used range as you would observer using Ctrl+End dim rowCnt as long rowCnt = activesheet.Cells.SpecialCells(xlLastCell).row Take a look at http://www.mvps.or...

new email notification yet no email
I have a problem here at work. couple of my users are complaining that they will get the new email notification pop up and when they select "would you like to read it now" and hit yes, they get the following message: Error, email has moved or deleted. They get this about 3 times in a couple minute period thru out the day. I have ran adware and spybot to see if it was sometype of spyware program, checked for viruses, but they come up clean. It is doing it on office XP and office 2000 on w2kpro stations. Any help or ideas on this would be greatly appriciated. Thanks Is ...

new error message whern closing out of Money 98
New problem lately., When after I finished editing the money 98 program, , when I go to exit , ( the program is set to backup with out reminding), I get a small error box that pops up that says: " This operation cannot be performed." If I set the backup to navigate to individual folder on C: drive, it then wants me to save to a differant name, so therefore I have two files, in there one called '07 and the other called 07a'. BUT the 'save' button of backup with reminding me will save the backup to both files., one about a few seconds earlier than the other. No...

Setup new virtual directory in existing Reporting Services deployment
Hi We have RS running on a 2005 cluster. So, the dbs are in the clustered instance and the service runs on each node. We have one application using the service, which is configured to use the default web site, ie, the cluster name, in this case //<cluster_name>/reports I have another application that needs to use reporting services. This app has it's own web server already. Can I use the reporting services already running on the cluster, but setup with new virtual directory on the applications web-server. So, the databases would stay where they are on the cluster, but th...

Create new publication from existing
Hi, where is the ability to create new publication from existing file in 2007 like it was in 2003? Jan Not sure I understand the question. All the options are on the task pane when you open Publisher. I think you want to click "Apply a Template..." -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "Jan Kucera" <miloush@community.nospam> wrote in message news:ABBAF0D0-5287-484F-969D-6D8991979A80@microsoft.com... > Hi, > where is the ability to create new publication from existing file in 2...

searching database objects
Is there a way to search database objects for the presence of embedded text strings? Although I'm interested in searching all objects, queries are the priority. Thanks! So you want to search the query design for a field name (not the query results for a value stored in a field)? If so, see: Where is a field used? at: http://allenbrowne.com/ser-73.html The code lets you choose which object type(s) to look in. -- Allen Browne - Microsoft MVP. Perth, Western Australia Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps do...

Teh selected object could not be found
I am desperately trying to remove account shared rights from a user whose account has been deleted from the domain. I tried using the hotfix and registry key for this problem, but that one is specifically for Account Owner Rights. This user has shared rights to the account and I can not remove her or add anyone else to it. I get the following message: The Selected object could not be found. Verify that the object exists in the database and Active Directory. She is already out of the directory, how can I remove her manually from the database? I am thinkin of deleting her from the S...

Adding a new Category
I created a new department, and now I can't create ANY new categories. I get a message "Run-time error '94': Invalid use of Null" with the only choice being OK. Then, RMS shuts down. Any suggestions? In Manager | Database | Tender Types under the "Verification" tab, see if "Validation Text" is checked. It's been my experience that it will require you to have a Slip Printer. If no Slip Printer is found you will receive that error message. Craig "360" <360@discussions.microsoft.com> wrote in message news:4E601A7C-9419-4C29-...

Unable to create personal folder file
I started Outlook 2007 for the first time(I've been using Outlook Express). The start up stopped saying it could not open my default e-mail folders and told me to go to control panel mail. I did this but when I try to create an Office Outlook personal folder file, it said the service was not installed. I did an Office 2007 repair and rebooted. But still no luck. Start with a new mail profile and see if it works now; http://www.howto-outlook.com/faq/newprofile.htm -- Robert Sparnaaij [MVP-Outlook] Coauthor, Configuring Microsoft Outlook 2003 http://www.howto-outlook.com/ ...

Set DataNavigateUrlField to contained custom object property
I have a GridView bound to a collection of custom objects (e.g., Department). Each Department object contains a custom object (e.g., Manager). I want to use a property on the Manager object (e.g., Id) for the DataNavigateUrlFields property on my HyperLinkField, but setting DataNavigateUrlFields equal to Manager.Id (i.e., DataNavigateUrlFields="Manager.Id") does not work. How can I go about doing this? I have successfully exposed the Manager.Id property as a property on the Department object itself, but would prefer a more elegant solution, if possible. ...

Missing menu options
I was messing around the other day trying to change the format of a document in word and now somehow I have managed to change the available options in my drop-down menus, such as the Word menu in OS X. The "save," "save as," and, "print" options, among others, are totally missing. Is there a way to get these back or to reset word to have the default settings? I have trashed my preferences from the library/preferences folder and it didn't do anything. Thanks. In article <bcbb9b66.0402090640.23a67ecd@posting.google.com>, jkilligan@yahoo.com (jkilliga...

On Change, copy new data
I'm creating a report of only the changes that have been made in the database since the last publication of the report. I don't want to list all the information about a contact, only what has changed. I've created hidden text boxes corresponding with the contact information. (i.e. - txtAddress has a corresponding txtAddressChange.) I have in the txtAddress (and all the other text boxes) in the On Change, code to copy the data to the new text box. (Me.txtAddressChange = txtAddress). However, this copies the old information and I'm wanting it to copy the new information. I...

Turning Off Auto Fill Options
How do you turn of the Auto Fill Options so when I copy a cell over that little popup stops popping up and offering "Copy Cells", "Fill Series", etc. It's more annoying than it's worth. I'm using Excel 2003 and thanking you in advance for your help. John Don't use the right mouse button to copy over or down. Use the left mouse button. Gord Dibben MS Excel MVP On 9 Feb 2007 11:12:30 -0800, "John13" <johnasmith13@gmail.com> wrote: >How do you turn of the Auto Fill Options so when I copy a cell over >that little popup stops popp...

Email Tracking
Hi there. With Version 1.2 we could send out an email using MSCRM, and the response from the customer (assuming the token was left alone in the subject line) would also be aggregated to the history of activities - without user intervention. (So I would see both emails tracked against the customer's activity history without having to do anything) With version 3.0, the responses via email only come through to outlook and do not have the tracking done automcatically in MSCRM, eventhough the token is left inside the subject line. Users now need to manually click on "track in CRM"...

specifying which subdatasheet in Access 2003
In Access 2003, I have a form with four distinct subforms. When I open the main form in datasheet view, the first subform is available as a subdatasheet. 1) How do a specify a different subform to be used as the subdatasheet? 2) The subform originally opens in form view when the subdatasheet is expanded and it appears. Is there a way to have it appear in datasheet view instead? Thanks. Neil wrote: | In Access 2003, I have a form with four distinct subforms. When I open | the main form in datasheet view, the first subform is available as a | subdatasheet. | | 1) How do a s...

new to access
my client wants an application to behave like this website: http://ericshin.com/orchestrations.php I would think that access would be the ideal application. Any direction to get me started would be greatly appreciated. -thanks On 26 Feb 2010 17:16:24 GMT, i dude eclair <br25354@aol.co.uk> wrote: >my client wants an application to behave like this website: > >http://ericshin.com/orchestrations.php > >I would think that access would be the ideal application. > >Any direction to get me started would be greatly appreciated. > >-thanks An...

Creating Application User Help File
I need some assistance creating a Help File for my application so that a user can get instant assistance if they need within the application. I've already documented some of the application procedures within MSWord, but do not know if this is the right approach. Thanks for your help Hi Jasmine, It isn't that easy.... To document your procedures, use something like MZTools to document each procedure in its code module. You do that for the benefit of future developers who must maintain your code. Do a good job, you're likely to be the future developer who benefits most. Y...

Help in creating a formula #2
Hi, Here is my scenario, I am attempting to export data from another program into excel (financial data) that changes daily making my cell location for totals change is there a formula that can combat that??? to eliminate data entry time!!! :P Hi Place your totals into table header, and calculate them using dynamic ranges. An example: Your data are on sheet MyData in range A5:E5 and down. In range A4:E4 are column headers. Rows 1:3 are empty. Column A contains some identifier (Name or Date etc.) and is never is empty, whenever there are any data in row. Columns C:E contain numeric data...

new account from same bank
I have a new cc with the same bank. When I use the bank's download tool, Money posts the new transactions to the closed account. How do I direct the transactions to the new account? Mark the closed account as "not online" and change the account name/number. -- Regards Bob Peel, Microsoft MVP - Money For unofficial FAQs see http://money.mvps.org/ or http://umpmfaq.info/ I do not respond to any emails that I have not specifically asked for. <crw80@comcast.net> wrote in message news:8a762d55-01ef-43b0-bc93-bdf46725c123@f16g2000vbf.googlegroups.com... > I have a new ...

A new interthread queueing essay
I decided to reward myself for a successful (and on-time!) product ship (the first units were shipped this morning) by taking time to write an essay. What I had to do was come up with a simple, interthread communication mechanism that did NOT use PostMessage, because the high volume of messages killed the GUI by having its queue always full of data messages. Furthermore, I had to handle the case were the existing code base (some 40K lines of code) did not have to change substantially to accommodate this. More seriously than the enqueuing of messages was the handling of them; I'd already bu...

Can=?ISO-8859-1?Q?=92?=t remove New Subfolder and enclosed Templates/Alphabetical order?
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel Is there a way to remove any new subfolders that you put into Project Gallery? <br><br>I reset Project Gallery but it had no affect. <br><br>Also, is there a way to put the new subfolder in alphabetical order? The folder and the enclosed files are invisible and can't be removed but they do appear in Project gallery. <br><br>Can you help? ...

How do I create a broken x-axis in Excel?
I am trying to create a XY scatter graph and would like to have a broken x-axis. Any suggestions? Jump on the Internet and visit these guys: http://peltiertech.com/Excel/Charts/BrokenYAxis.html http://tushar-mehta.com/excel/newsgroups/broken_y_axis/tutorial/ -- Bernard V Liengme www.stfx.ca/people/bliengme remove caps from email "mokey24" <mokey24@discussions.microsoft.com> wrote in message news:13FAD5B6-D56B-4E51-A6C9-1F1FD3515C62@microsoft.com... >I am trying to create a XY scatter graph and would like to have a broken > x-axis. Any suggestions? ...

Workflows NOT working on Create
Hi, I have created over 32 diffrent workflows on the Microsoft CRM, these workflows are supposed to be triggered by selecting a value in a picklist that has been created on the Case form. Simply said, "When case is created If Case.Reason_For_Leaving = Death Then Task Appointment Email Etc End if" But this is not whats happening. Does the Administrator need to have a License in order for the Workflows to work? Rgds Jefferson First I'd check that the workflows have been activated, and that the CRM Workflow Service is running. If the above are OK, try a Wait for...