LINQ to XML Functional Construction - validating data passed to new XElement

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I hope this forum includes discussion of LINQ to XML.<br/>
The questions are asked at the very last.<br/>
I present the following commentary to help those who may help me understand the context of this task...<br/>
<br/>
MSDN Functional Construction (LINQ to XML)<br/>
http://msdn.microsoft.com/en-us/library/bb387019.aspx
//MSDN Example Code<br/>
XElement srcTree = new XElement("Root",<br/>
new XElement("Element", 1),<br/>
new XElement("Element", 2),<br/>
new XElement("Element", 3),<br/>
new XElement("Element", 4),<br/>
new XElement("Element", 5)<br/>
);<br/>
XElement xmlTree = new XElement("Root",<br/>
new XElement("Child", 1),<br/>
new XElement("Child", 2),<br/>
from el in srcTree.Elements()<br/>
where (int)el > 2<br/>
select el<br/>
);<br/>
Console.WriteLine(xmlTree);
//MSDN Example Code Output<br/>
<Root><br/>
<Child>1</Child><br/>
<Child>2</Child><br/>
<Element>3</Element><br/>
<Element>4</Element><br/>
<Element>5</Element><br/>
</Root>
// My Obervation: The MSDN code and output shows how to write a LINQ query inline allowing the compiler to control outputting or not outputting an XML element(s) that contain values.
My inline LINQ query that follows generates an empty XML <name /> element.<br/>
I hope somebody will clean up this query and help me learn how to use inline LINQ queries to validate the existence of data so the compiler WILL NOT generate an XML element at all if there is no data being passed to an instance of a new XElement as the following
shows...
I use the following code in a Console Application:
// Instantiate a Dictionary with some string data<br/>
var person = new Dictionary<string, string>();<br/>
person.Add("name", "Patrick Henry");<br/>
person.Add("phone", "888-888-8888");<br/>
person.Add("streetaddress", "10 Shadey Lane");
// Output verifies the person Dictionary contains string data<br/>
foreach (KeyValuePair<string, string> entry in person) <br/>
Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
Console.WriteLine("");
<br/>
Console.WriteLine("");
// output of person Dictionary:<br/>
name: Patrick Henry<br/>
phone: 888-888-8888<br/>
streetaddress: 10 Shadey Lane?
// My Functional Construction (the outputs undesired empty XML elements)<br/>
XElement contacts = new XElement("contacts",<br/>
new XElement("contact",
<br/>
new XElement("name", from n in person["name"] where person["name"].Length != 0 select n),<br/>

new XElement("phone", "206-555-0144"),<br/>

new XElement("streetaddress", "123 Main St"))<br/>
);
// When person Dictionary contains string<br/>
person.Add("name", "Patrick Henry");<br/>
// Functional Construction outputs:<br/>
<name>Patrick Hanry</name>
// When person Dictionary contains empty string<br/>
person.Add("name", "");<br/>
// Functional Construction outputs:<br/>
<name />
Q1: How do I correctly write the Functional Construction and LINQ query so the empty <name /> element is not generated when there is no data for that element?

View the full article
 
Back
Top