You know you're a geek when it's not even 7AM but you've already spent half the morning reading a whitepaper about Microsoft's plans to integrate XML and relational query language functionality into the .NET Framework with Linq. C# 3.0 is going to be hot.
-
Creating an XML document
XDocument contactsDoc =
new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XComment("XLinq Contacts XML Example"),
new XProcessingInstruction("MyApp", "123-44-4444"),
new XElement("contacts",
new XElement("contact",
new XElement("name","Patrick Hines"),
new XElement("phone", "206-555-0144"),
new XElement("address",
new XElement("street1", "123 Main St"),
new XElement("city", "Mercer Island"),
new XElement("state", "WA"),
new XElement("postal", "68042")
)
)
)
);
-
Creating an XML element in the "http://example.com" namespace
XElement contacts = new XElement("{http://example.com}contacts");
-
Loading an XML element from a file
XElement contactsFromFile = XElement.Load(@"c:\myContactList.xml");
-
Writing out an array of Person objects as an XML file
class Person {
public string Name;
public string[] PhoneNumbers;
}
var persons = new [] { new Person
{Name="Patrick Hines",
PhoneNumbers = new string[]
{"206-555-0144", "425-555-0145"}
},
new Person {Name="Gretchen Rivas",
PhoneNumbers = new string[]
{"206-555-0163"}
}
};
XElement contacts = new XElement("contacts",
from p in persons
select new XElement("contact",
new XElement("name", p.Name),
from ph in p.PhoneNumbers
select new XElement("phone", ph)
)
);
Console.WriteLine(contacts);
-
Print out all the element nodes that are children of the <contact> element
foreach (x in contact.Elements()) {
Console.WriteLine(x);
}
-
Print all the <phone> elements that are children of the <contact> element
foreach (x in contact.Elements("phone")) {
Console.WriteLine(x);
}
-
Adding a <phone> element as a child of the <contact> element
XElement mobilePhone = new XElement("phone", "206-555-0168");
contact.Add(mobilePhone);
-
Adding a <phone> element as a sibling of another <phone> element
XElement mobilePhone = new XElement("phone", "206-555-0168");
XElement firstPhone = contact.Element("phone");
firstPhone.AddAfterThis(mobilePhone);
-
Adding an <address> element as a child of the <contact> element
contact.Add(new XElement("address",
new XElement("street", "123 Main St"),
new XElement("city", "Mercer Island"),
new XElement("state", "WA"),
new XElement("country", "USA"),
new XElement("postalCode", "68042")
));
-
Deleting all <phone> elements under a <contact> element
contact.Elements("phone").Remove();
-
Delete all children of the <address> element which is a child of the <contact> element
contacts.Element("contact").Element("address").RemoveContent();
-
Replacing the content of the <phone> element under a <contact> element
contact.Element("phone").ReplaceContent("425-555-0155");
-
Alternate technique for replacing the content of the <phone> element under a <contact> element
contact.SetElement("phone", "425-555-0155");
-
Creating a contact element with attributes multiple phone number types
XElement contact =
new XElement("contact",
new XElement("name", "Patrick Hines"),
new XElement("phone",
new XAttribute("type", "home"),
"206-555-0144"
),
new XElement("phone",
new XAttribute("type", "work"),
"425-555-0145"
)
);
-
Printing the value of the <phone> element whose type attribute has the value "home"
foreach (p in contact.Elements("phone")) {
if ((string)p.Attribute("type") == "home")
Console.Write("Home phone is: " + (string)p);
}
-
Deleting the type attribute of the first <phone> element under the <contact> element
contact.Elements("phone").First().Attribute("type").Remove();
-
Transforming our original <contacts> element to a new <contacts> element containing a list of <contact> elements whose children are <name> and <phoneNumbers>
new XElement("contacts",
from c in contacts.Elements("contact")
select new XElement("contact",
c.Element("name"),
new XElement("phoneNumbers", c.Elements("phone"))
)
);
-
Retrieving the names of all the contacts from Washington, sorted alphabetically
from c in contacts.Elements("contact")
where (string) c.Element("address").Element("state") == "WA"
orderby (string) c.Element("name")
select (string) c.Element("name");